0

currently i have php validation as follows

if ($_FILES["file"]["size"] > 5120) 
{
$_SESSION['error'] = 'Upload FAILED, file is too large !';
header("Location: upload.php");
exit;
}

But this does not seem to stop any large files being uploaded! any help much appreciated!!

j08691
  • 204,283
  • 31
  • 260
  • 272
neeko
  • 1,930
  • 8
  • 44
  • 67
  • 2
    I think that the filesize is only checked after the upload. So what might be happening is that the file is uploaded, it's too large, so the user sees the error. But the file is there on the server by that point. – andrewsi Oct 03 '12 at 20:00

2 Answers2

1

Use this to restrict the upload file size :

$max_size = /* whatever */; //File Size in Bytes

if(filesize($_FILES['userfile']['tmp_name']) > $max_size) {
      die('The file you uploaded is too large.');
    }
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • 1
    Note: this won't prevent the file from being uploaded. The user still has to wait for the whole thing to transfer, and you incur the bandwidth usage involved. – ceejayoz Oct 03 '12 at 20:13
  • thank you for your reply, how would i check the size of the file and prevent it being uploaded? – neeko Oct 03 '12 at 20:16
  • @ceejayoz Adding it as a additional info to my answer, permission please :) – Mr. Alien Oct 03 '12 at 20:16
  • 2
    @neeko use javascript...http://stackoverflow.com/questions/7133870/check-filesize-before-upload – Mr. Alien Oct 03 '12 at 20:17
  • @Mr.Alien how do i implement the javascript? – neeko Oct 03 '12 at 20:22
  • pass an id to your element, and retrieve the info else use this jQuery solution http://stackoverflow.com/questions/7497404/find-out-file-size-before-uploading-using-jquery-ajax – Mr. Alien Oct 03 '12 at 20:24
0

Use upload_max_filesize

Entry can be set in php.ini, .htaccess, httpd.conf or .user.ini

http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize

Philip Whitehouse
  • 4,293
  • 3
  • 23
  • 36