首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图片上传表单错误码为2,为什么?

图片上传表单错误码为2,为什么?
EN

Stack Overflow用户
提问于 2016-04-28 14:32:56
回答 1查看 54关注 0票数 0

我正在为我的新网站做一个注册表。在我表单的图片上传部分,图片似乎不会无缘无故地上传。我已经看过所有东西两次了,并且三次仍然将$_FILES['dp']['error']设置为2。

我尝试上传的文件只有123kb,但它仍然显示错误2,并且没有上传。为什么?

我的HTML:

代码语言:javascript
复制
<form enctype="multipart/form-data" class="form-signin" role="form" method="post" action="addit.php">
<h2 class="form-signin-heading" align="center">Sign Up</h2>
<input value="" name="dp" type="file" class="form-control">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>

PHP:

代码语言:javascript
复制
$target_dir = "img/";

$target_file = $target_dir . basename($_FILES["dp"]["name"]);
echo $target_file;
echo $_FILES["dp"]["tmp_name"];
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["dp"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["dp"]["name"]). " has been  uploaded.";
} else {
    echo $_FILES['dp']['error'];
}
}

php.ini配置:

代码语言:javascript
复制
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = "d:/wamp/tmp"

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 64M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
EN

回答 1

Stack Overflow用户

发布于 2016-04-28 14:39:37

请使用代码作为参考

代码语言:javascript
复制
$imgname = basename($_FILES["fileToUpload"]["name"]);
$target_dir = "../../images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "</br>File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "</br>File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "</br>Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "</br>Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "</br>Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "</br>Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "</br>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "</br>Sorry, there was an error uploading your file.";
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36906976

复制
相关文章

相似问题

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