[英]Error in php error handling (file upload)
当上传的图片大小超过3MB时,我想显示一条错误消息。 这是我当前的代码。 当图像超过3MB时,它应该发出一条错误消息,但是它什么也不做。 我的代码有什么问题?
if ( $_FILES['file']['size'] != 0 )
{
//image check start
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 3072000))
//image check end
{
if (file_exists($upload_path."/".$_FILES["file"]["name"]))
{
$file_tmp = $_FILES["file"]["name"];
} //Link if there is already a file with identical file name
else
{
$photoid = $upfile_idx.".".substr($_FILES['file']['name'],-3);
move_uploaded_file($_FILES["file"]["tmp_name"], $upload_path."/".$photoid);
$file_tmp = $photoid ;
} //Upload the image file into upload folder and generate an id for the image
}
else
{
error("Maximum image size exceeded or invalid file format.");
}
}
//insert $file_tmp into database here
----------
Error code (added later)
function error($msg)
{
echo "<script>alert(\"$msg\");history.go(-1);</script>";
exit;
}
我发现了什么问题。 在我的php.ini文件中,存在“ upload_max_filesize = 3M”,这显然是导致所有问题的原因。 当我将其更改为“ upload_max_filesize = 4M”时,一切正常。 谢谢大家的帮助。
以下内容对您有好处。
<?php if ( $_FILES['file']['size'] != 0 ) { //image check start if(!in_array($_FILES["file"]["type"], array("image/gif", "image/jpeg", "image/png", "image/pjpeg"))) { error("File should be a JPG/JPEG/PNG/PJEPG only"); return; } if($_FILES["file"]["size"] > 3145728) { error("File should be less than 3 MB"); return; } #Rest of your upload code should go below here. } ?>
3145728转换为3 MB字节,因为$ _FILES [“ file”] [“ size”]给出了要上传的文件的大小(以字节为单位)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.