Reputation: 618
How do I display "file size is greater than the specified" and "Please upload only Image" message to user? I tried with the below code
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.";
but that didn't work.
<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>
Here is my php code below. It works by adding else
{
echo "Invalid file";
}
at the end but i wanted to show individual error to the user...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!";
}
}
}
?>
Upvotes: 1
Views: 7475
Reputation: 921
I am not sure if you wanna go for client side validation for file upload, but if you want to do so, there is a nice jQuery plug-in.
http://adamsanderson.github.com/jQuery-File-Validator/
It will be helpful. You can customize the code to validate for either of or both of - size and type of file.
Upvotes: 0
Reputation: 658
I think precedence should be like
so i prefer following
<?php
if ($_FILES["file"]["error"] > 0) {//Checking file has error or not
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"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) { //Checking file is an image or not
if ($_FILES["file"]["size"] < 2097152) { //checking file size is less than 2MB or not
if (file_exists("upload/" . $_FILES["file"]["name"])) { //Checking file already exists or not
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!";
}
} else {
echo "ERROR: Your file was larger than 2MB in file size.";
}
} else {
echo "Please upload only Image.";
}
}
?>
Upvotes: 0
Reputation: 15045
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
There you are checking to see if the filesize is less than the max allowed. So if it is more that if() statement will return FALSE and the rest of code will not run. That being the case inside that if() block you then do:
if ($_FILES["file"]["size"] > 2097152)
Which you check if it is more, but you have already validated the size with the first if() so a file too large won't trigger the error as it would have already failed the first if()
So to fix it you would remove this line, so you can output the error individually:
&& ($_FILES["file"]["size"] < 2097152)
Upvotes: 1