首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >文件上传与php问题

文件上传与php问题
EN

Stack Overflow用户
提问于 2018-08-16 02:43:22
回答 2查看 51关注 0票数 -1

我正在尝试让文件上传元素在我们的网页上工作,这样客户就可以上传图片给我们了。我从code pen下载了一个漂亮的图片上传元素

然后我从w3学校得到了PHP代码(完整的代码在底部):

https://www.w3schools.com/php/php_file_upload.asp

浏览器在第一次检查“getimagesize”时返回错误。上面写着:

警告:image.Sorry大小():文件名在第10行的W:\xampp\htdocs\image-upload-preview\upload.php中不能为空文件不是image.Sorry,您的文件未上载。

代码语言:javascript
复制
<form action="upload.php" method="post" enctype="multipart/form-data">
  <h1>Image-upload with preview</h1>
<div class="wrapper">
  <div class="box">
    <div class="js--image-preview"></div>
    <div class="upload-options">
      <label>
        <input type="file" class="image-upload" accept="image/*" name="fileToUpload" />
      </label>
    </div>
  </div>


if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}

}

EN

回答 2

Stack Overflow用户

发布于 2018-08-16 03:46:02

你得到的警告并没有破坏你的代码。要修复警告,请检查您调用的文件是否已设置为

代码语言:javascript
复制
$checkfile = isset($_FILES['fileToUpload']['tmp_name']) ? $_FILES['fileToUpload']['tmp_name'] : '';

然后

代码语言:javascript
复制
$check = getimagesize($checkfile);

您遇到的问题是文件是空的,所以它正在运行else语句并使$uploadOk = 0。尝试找出图像的位置,并确保将其放入$_FILES的‘fileToUpload数组中。

票数 0
EN

Stack Overflow用户

发布于 2018-08-17 00:25:51

感谢您的回复。我从头开始编写了一个更好的教程here我还必须解决php,ini中的文件大小限制(然后忘记重启apache),但我最终让它工作了。下面是我的php代码:

代码语言:javascript
复制
<?php

if (isset($_POST['submit'])){
//the name 'file' is from the HTML
//$_FILES is a super global
$file = $_FILES['file'];

//creating variables for the file attributes
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];

//creates an array for the name and extension
$fileExt = explode('.', $fileName);

//creates variable for the lower case extension, from the end of the array
$fileActualExt = strtolower(end($fileExt));

//allowed extensions
$allowed = array('jpg','jpeg','png','tif');

//checks
if(in_array($fileActualExt,$allowed)){ 
        if($fileError === 0){
            if($fileSize < 25000000){
                //creates a new file name
                $fileNameNew = uniqid('',true).".".$fileActualExt;
                //set file destination
                $fileDestination = 'uploads/'.$fileNameNew;
                //moves from temp to proper destination
                move_uploaded_file($fileTmpName,$fileDestination);
                //goes back to previous page upon sucess
                header("Location: index.html?uploadsucsess");
            }else{
                echo "File must be less than 25 mb";
            }
        } else{
            echo "There was an error uploading your file";
            echo $fileError;
        }
    } else{
        echo "Invalid File Type Must be a JPG, PNG, or TIF";
    }

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51864604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档