当上传的图像大小超过 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”时,一切正常。感谢大家的帮助。