我想在上传的图像大小超过 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"时,一切正常。谢谢大家的帮助。
以下一些东西应该对你有好处。
<?phpif> 3145728) { 错误("文件应小于 3 MB"); 返回; } 您的上传代码 #Rest 应在此处下方。}?>
3145728转换为 3 MB 的字节,因为 $_FILES["文件"]["大小"] 给出了以字节为单位的上传文件的大小。