我想在上传的图片大小超过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'时,一切正常。谢谢大家的帮助。
答案 0 :(得分:0)
以下内容应该对你有好处。
<?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”]给出了以字节为单位上传的文件的大小。