如何向用户显示“文件大小大于指定”和“请仅上传图片”消息?我尝试使用以下代码
if ($_FILES["file"]["size"] > 2097152) { // if file is larger than we want to allow
echo "ERROR: Your file was larger than 2MB in file size.";
但这没有用。
<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" ><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
下面是我的 php 代码。它通过else
{
echo "Invalid file";
}
在最后添加来工作,但我想向用户显示个别错误...txs
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if ($_FILES["file"]["size"] > 2097152) { // if file is larger than we want to allow
echo "ERROR: Your file was larger than 2MB in file size.";
}
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "your photo has been uploaded successfully!";
}
}
}
?>