4

As far as I know $_FILES["fieldname"]["size"] contains the file size after the file has been uploaded.

In Perl, you can read the raw file data very easy chunk-wise and that way determine if the file is too big before it has been fully uploaded.

Is there an easy way to do just the same in PHP?

7
  • 1
    stackoverflow.com/questions/11514166/… Commented Dec 9, 2013 at 10:33
  • 2
    Nope. Your PHP script is only started when the upload to the server is already complete. Commented Dec 9, 2013 at 10:33
  • how does the file gets to the server? if you use a html-formular you could limit the filesize via html attribute maxlength, which limits the number of bytes of the input... Commented Dec 9, 2013 at 10:37
  • I already check the file size on the client side. However, I thought it would be nice if you could discard the fileupload if a certain number of bytes has been exceeded. Okay, my file upload is for small pictures, can I somehow set an individual max file size in php.ini for a paticular script? Commented Dec 9, 2013 at 10:42
  • The basic problem is that the HTTP connection and upload are handled by the web server, not by PHP code. You have php.net/manual/en/session.upload-progress.php, but this needs to be invoked explicitly by a separate HTTP request. You could use a script which attaches to a port and explicitly handles HTTP connections "manually", which gives you full freedom, but is probably overkill. If you goal is to simply limit the maximum upload size, there's an .ini setting for that though in both PHP and your web server. Commented Dec 9, 2013 at 10:42

2 Answers 2

1

You can change the max size in the php.ini config file, but it will affect all your file uploads :

upload_max_filesize 10M

If you're using a .htaccess file, you can also write like that :

php_value upload_max_filesize 10M

Take a look at the doc

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot reach the file on the client from the server without copying it. What you can do is inspect the file using javascript on the client side, and decide then whether to upload or not.

Basically, read your file into a FileReader, and check the size attribute.

https://stackoverflow.com/a/4307882/326154 has some good answers.

2 Comments

How can you inspect a file on filesystem via javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.