0

i wanted to know what's the best way to make my 3mb photo smaller on upload i have this upload form but for some reason it wont upload images over 2mb i keep getting "file is too small or large" PHP ONLY

$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
$max_file_size = 2000000 * 1234567; 
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ( isset($_FILES['image']) ) {
        if (! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size) {
            $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
            if (in_array($ext, $valid_exts)) {
                    if (!file_exists('photos/' )) {
                        mkdir('photos/' , 0777, true);
                        }
                    $path = 'photos/' . uniqid() . '.' . $ext;
                    $size = getimagesize($_FILES['image']['tmp_name']);


                    $x = (int) $_POST['x'];
                    $y = (int) $_POST['y'];
                    $w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
                    $h = (int) $_POST['h'] ? $_POST['h'] : $size[1];

                    $data = file_get_contents($_FILES['image']['tmp_name']);
                    $vImg = imagecreatefromstring($data);
                    $dstImg = imagecreatetruecolor($w, $h);
                    imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $w, $h, $w, $h);
                    imagejpeg($dstImg, $path);
                    imagedestroy($dstImg);
                    /*print_r($path);*/



                } else {
                    echo 'unknown problem!';
                } 
        } else {
            echo 'file is too small or large';
        }
    } else {
        echo 'file not set';
    }
} else {
    echo 'bad request!';
}
  • I'm just curious, but... in this part: `$max_file_size = 2000000 * 1234567;` what are those numbers doing? – summea Apr 02 '14 at 20:05
  • possible duplicate of [How to compress an image via Javascript in the browser?](http://stackoverflow.com/questions/14672746/how-to-compress-an-image-via-javascript-in-the-browser) – Brad Christie Apr 02 '14 at 20:06
  • How's the php.ini look? What's the maximum file size you're allowed there? If that's set to 2MB max, that may be your limit there, not anything with your script directly. http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size – Gyhth Apr 02 '14 at 20:06
  • its supposed to be the bytes, that would add up to the total bytes –  Apr 02 '14 at 20:06
  • no not a duplicate im asking php only not javascript –  Apr 02 '14 at 20:07
  • max files size is 10M –  Apr 02 '14 at 20:07
  • 2,000,000 * 1,234,567 = 2.47e+12, roughly. That's a mighty big image file. I'd have thought 10000000 would be big enough. –  Apr 02 '14 at 20:09
  • `$max_file_size = 2000000 * 1234567;` So your max file size is 2.2TB? 1MB is `1024*1024` or `1,048,576`, BTW. – Sammitch Apr 02 '14 at 20:10
  • well i wanted to have $max_file_size be big enough just in case the file size is bigger than expected. –  Apr 02 '14 at 20:10
  • well stop talking about max_file_size if its not going to solve the problem, please –  Apr 02 '14 at 20:11
  • @Sammitch That could also be written as `1024^2` – Kermit Apr 02 '14 at 20:15
  • @user2865738: You can't in php if you can't upload over 2mb. it _has_ to be done client-side otherwise you're going to continue to battle a server-side threshold. – Brad Christie Apr 02 '14 at 20:17
  • @Kermit not in php! `^` is one of the bitwise operators. Forget which, though. `pow(1024,2)` or `pow(2,20)`. :P – Sammitch Apr 02 '14 at 20:20

1 Answers1

1

$_FILES['image']['error'] is an integer code where UPLOAD_ERR_OK is 0. Docs

Replace:

if (! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size)

With:

if ( ($_FILES['image']['error'] == UPLOAD_ERR_OK) && ($_FILES['image']['size'] < $max_file_size) )

and I would suggest not even bothering to check the size of the file if you don't care how large the file may be. There's no point in arbitrarily setting an impossible max file size like that when you're already constrained by:

  1. post_max_size and upload_max_size in php.ini.
  2. Potentially the post size constriants of your HTTP server/CGI gateway. [IIS springs to mind]
  3. The 2GB upload limit imposed by shortcomings in PHP's core. [file size is tracked in a signed 32bit integer regardless of your architecture.
Sammitch
  • 25,490
  • 6
  • 42
  • 70