-1

I have written below lines of code

<?php

$uniqId = uniqid('file_');
$root = $_REQUEST['root'];

$target_file = "uploads/".basename($_FILES["file"]["name"]);

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if ( 0 < $_FILES['file']['error'] )
{
    echo 'Error';
}
else if ($_FILES["file"]["size"] > 2097152) 
{
  echo "SizeError";
}
else if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "docx" && $imageFileType != "pdf") 
{
  echo "ExtensionError";
}
else 
{   
   if( move_uploaded_file($_FILES['file']['tmp_name'], $root.$uniqId.".".$imageFileType))
   {
       echo $uniqId.".".$imageFileType;
   }
}

?>

If I upload 5 mb file, the code is jumped to simple "Error" condition. I want that it should display SizeError. Please help!!!

Nida Amin
  • 735
  • 1
  • 8
  • 28
  • else if ($_FILES["file"]["size"] > 5242880) – Tejas Mehta Apr 24 '18 at 10:28
  • https://stackoverflow.com/questions/14758191/how-to-use-filesfilesize?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Tejas Mehta Apr 24 '18 at 10:29
  • 1
    According to my opinion `if ( 0 < $_FILES['file']['error'] ) { echo 'Error'; }` is not needed at all – Alive to die - Anant Apr 24 '18 at 10:32
  • the question is not about size, the main issue is error message being displayed. – Nida Amin Apr 24 '18 at 10:32
  • set 5000000 bytes in condition if you are strongly checking condition @nida – Tejas Mehta Apr 24 '18 at 10:32
  • May I suggest you take a look at my answer to this question: [Full Secure Image Upload Script](https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921). As it is right now, your script is extremely insecure. So much, that someone is able to harm your server and your visitors. My answer explains in detail on how to tackle all of this. And at the end, you will find a download link to library which is very easy to use. This also includes limiting file sizes and handling multiple files at the same time. – icecub Apr 24 '18 at 10:32
  • Error message is not printing because your condition is wrong – Tejas Mehta Apr 24 '18 at 10:33
  • yes in this line if ($_FILES["file"]["size"] > 2097152) { echo "SizeError. } – Nida Amin Apr 24 '18 at 10:36
  • Have a look at https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size - I think you file isn't even uploaded – Nico Haase Apr 24 '18 at 10:49

4 Answers4

1

This is an alternative way to get size

if (filesize($target_file) > 2097152) 
{
  echo "SizeError";
}

But firstly I think there is an error UPLOAD_ERR_INI_SIZE at $_FILES['file']['error']. UPLOAD_ERR_INI_SIZE=1 You can increase it in php.ini. Add or modify this in your php.ini for example yo increase max_file_size = 25mb:

upload_max_filesize = 25M

After modifying php.ini your code should work too:

if ( 0 < $_FILES['file']['error'] )
{
    echo 'Error';
}
else if ($_FILES["file"]["size"] > 2097152) 
{
  echo "SizeError";
}

To check your php.ini settings call:

echo phpinfo();

You will see your settings, find upload_max_filesize it's 2mb as default value. Looks like this:

enter image description here

Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15
  • Not my downvote, but if your answer includes _"Try this"_, it's very likely not an answer to the question. You didn't test it. You didn't verify if it'll solve the issue. You're taking a gamble. – icecub Apr 24 '18 at 10:37
  • @NidaAmin I have improved my answer, I think i found solution – Alexey Usachov Apr 24 '18 at 11:03
0

check this condition first if ($_FILES["file"]["size"] > 2097152) then check others.

You probably get the UPLOAD_ERR_INI_SIZE at $_FILES['file']['error'] which is qual to 1. You can increase it in php.ini. Change this in your php.ini:

upload_max_filesize = 25M
Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15
0

The reason you're getting "error" is because of this line:

if ( 0 < $_FILES['file']['error'] )

If you read the documentation here: PHP: Upload Error Messages Explained, then you'll notice that there are several values that can be returned with $_FILES['file']['error']. It returns 0 when there are no errors. But it returns 1 if

The uploaded file exceeds the upload_max_filesize directive in php.ini.

And it returns 2 if:

The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

In both cases 0 is smaller than 1 or 2. So your script is returning you "Error". Because the condition evaluates to true.

You need to either change the condition, or check for the file size first.

icecub
  • 8,615
  • 6
  • 41
  • 70
  • you are right but i want that this if ($_FILES["file"]["size"] > 2097152) should work – Nida Amin Apr 24 '18 at 10:50
  • now i will eliminate first condiion but the condition if ($_FILES["file"]["size"] > 2097152) does not work – Nida Amin Apr 24 '18 at 10:51
  • @NidaAmin Just change `if ( 0 < $_FILES['file']['error'] ){ .. } else if ($_FILES["file"]["size"] > 2097152)` to `if ($_FILES["file"]["size"] > 2097152){ ... } else if ( 0 < $_FILES['file']['error'] ){ .. }` and you're good to go. As I said, just check for the file size first, then for errors. – icecub Apr 24 '18 at 10:53
  • @NidaAmin Also, make sure you get _meaningfull_ errors returned. It's cool to echo "error", but that's not telling you anything. It doesn't tell you _what_ the error is. Instead, try something like `echo "Error: ". $_FILES['file']['error'];`. Now you'll see which error code is being returned. That can tell you what exactly is going wrong. – icecub Apr 24 '18 at 10:56
  • if ($_FILES["file"]["size"] > 2097152) if i use this condition first, it still does not throw SizeError message upon upload of large file than 2097152 – Nida Amin Apr 24 '18 at 11:05
  • @NidaAmin Like I said, make sure you get _Meaningfull errors_. I can't help you if you tell me "it doesn't work". I'm not some wizard that magicly knows _what_ exactly doesn't work. – icecub Apr 24 '18 at 11:53
0

I was facing the same issue,and got the solution. Codeigniter allows you to upload the image of size less than 2mb.but if you want to upload the image of 5 mb than you should add this line in your .htaccess file.

php_value upload_max_filesize 20M  

After adding this line in your .htaccess file,your code will work fine.