0

How do I verify that a file isn't too big to upload in php before uploading it to the temp folder?

This line:

if ($_FILES['file']['size'] <= 10485760)

will not work because the file gets uploaded to the temp directory first. I already have an Ajax solution that works perfectly. I am trying to code for when scripting is disabled.

According to php.net I can prevent a user from having to wait for a file to upload before receiving an error message stating that it's too big by using this line:

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

but that does not work. All it does it change the maximum file size to 30000.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
  • http://stackoverflow.com/questions/7497404/get-file-size-before-uploading – Mihai Mar 03 '16 at 15:56
  • 1
    How would PHP, running on the server, know whether the file is too big without it first being on the server? – CD001 Mar 03 '16 at 15:56
  • Filegetsize() use this function – Muhammed Mar 03 '16 at 15:57
  • @Mihai *"I already have an Ajax solution that works perfectly. I am trying to code for when scripting is disabled."* – Funk Forty Niner Mar 03 '16 at 15:57
  • The php.ini settings won't stop the file being uploaded, but they will deal with it immediately before it starts executing your php – Mark Baker Mar 03 '16 at 15:58
  • http://stackoverflow.com/a/11514290/ ***"Server Side Upload Canceling"*** – Funk Forty Niner Mar 03 '16 at 15:58
  • What do u mean scripting disabled? – Muhammed Mar 03 '16 at 15:59
  • @MuhammedM. he means if user disables JS. He want to use a serverside method not client-side. – Funk Forty Niner Mar 03 '16 at 16:00
  • My method is server side, when we say php it is server side, nothing client side – Muhammed Mar 03 '16 at 16:01
  • @MuhammedM. read the question again, he's basically after a client-side solution that's **not** JavaScript ... which I don't think exists yet. – CD001 Mar 03 '16 at 16:02
  • **A:** Basically you can't avoid it. In a way you can, but by the time the server checks to see if it's more than what is allowed, the person sending the file already spent the time uploading it and that may not wash very well if the limit is say... 40 mb and they uploaded a 50mb file. – Funk Forty Niner Mar 03 '16 at 16:05
  • @Fred-ii- according to the PHP manual I can avoid it, but I tried their solution and it won't work for me. http://php.net/manual/en/features.file-upload.post-method.php#example-341 – Dan Bray Mar 03 '16 at 16:57

1 Answers1

1

As users have stated in the comments, this cannot be done in PHP. The PHP manual is wrong, as it clearly states I can prevent a user from having to wait for the upload to complete before giving them an error because the files too big by using this line:

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

In future, I won't use php.net as a reference. I'll just stick to using Stack Overflow.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70