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?

Simulant
  • 19,190
  • 8
  • 63
  • 98
  • 1
    http://stackoverflow.com/questions/11514166/check-file-size-before-upload –  Dec 09 '13 at 10:33
  • 2
    Nope. Your PHP script is only _started_ when the upload to the server is already complete. – CBroe Dec 09 '13 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... – helle Dec 09 '13 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? –  Dec 09 '13 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 http://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. – deceze Dec 09 '13 at 10:42
  • Similar to [this question](http://stackoverflow.com/questions/17340566/check-file-type-flv-before-php-upload-file-in-temporary-folder-by-reading-only). – Burhan Khalid Dec 09 '13 at 10:43
  • @deceze: Yes, I thought I'd write an Perl/FastCGI app for the file upload, but it would be in fact overkill, given its purpose (file upload for small pictures). –  Dec 09 '13 at 10:46

2 Answers2

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

onionpsy
  • 1,502
  • 11
  • 15
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.

Community
  • 1
  • 1
Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59