skip to Main Content

I’ve been battling with a script to upload photos for user comments. Needless to say, I’m new to PHP. I have it so it confirms the file is an image and then continues to check its size. It’s all working except when the image exceeds the size I’ve set. It does absolutely nothing. Here’s my code:

ini_set("display_errors",1);
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
$date = date('m-d-Y_h.i.s_a', time());
$target_path = "/home/SOME_USER/WEB/SOME_SITE/comment/uploading/uploads/";
$filename = $date. "_" .basename( $_FILES['uploadedfile']['name']);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$test = preg_replace('/\.[^.\s]{3,4}$/', '', $filename);
$newName = bin2hex($test);
$target_path = $target_path."". "$newName.".$ext;
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
    $mime_type = mime_content_type($_FILES['uploadedfile']['tmp_name']);
    $allowed_file_types = ['image/png', 'image/jpeg', 'image/gif'];
    if (! in_array($mime_type, $allowed_file_types)) {
        $url = "./index.php" . "?&message=Not an image";
        header('Location: '.$url);
    } else {
        echo ('made it to this part ?');
        return proceed();
    }
}
function proceed() {
    global $target_path;
    global $newName;
    global $ext;
    echo 'made it here. . .';
    if(isset($_FILES['uploadedfile']['name'])) {
        if($_FILES['uploadedfile']['size'] > 3145728) { //3mb)
            echo ('made it here #1');
            $url = "./index.php" . "?&message=Too big !";
            header('Location: '.$url);
        } else {
                if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
                    $url = "./index.php" . "?&message=".$newName.".".$ext."";
                    header('Location: '.$url);
                } else{
                    echo('Made it here');
                    $url = "./index.php" . "?&message=Too big !";
                    header('Location: '.$url);
                }
        }
    }
}

I know the code is probably pretty ugly and could be written a lot better, and I’ll work on that when I get it working properly, but I’m stuck here. Again, everything is working except when the image is too large and it does absolutely nothing on the upload script. I’ve added some mile markers for debugging like I would in Python but it doesn’t seem to help in PHP.

Solved: In my form I had the file size limited to the same exact size as in my handler script. Changing the value in the form to just one byte larger fixed it.

2

Answers


  1. Chosen as BEST ANSWER

    I left out my form in the OP and the problem was in my form.

    <form enctype="multipart/form-data" action="./fupload.php" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="3145728"/> <!-- <- issue | Fixed by changing it to 3145730 -->
        <input name="uploadedfile" type="file" />
        <br>
        <button class="button1" type="submit" value="Upload File">Upload</button>
    </form>
    

    The issue is I matched the size name="MAX_FILE_SIZE" value="7145728" with handler. By increasing the size in my form by just 1 byte the problem was resolved. It was an accidental fix, but fixed it nonetheless. If you're going to set a size limit in your form (I'm not even sure if that's necessary or practical) and your PHP handler, make sure the size in the form is greater than your handler. I removed that line out of my form as it seems unnecessary and redundant with my PHP handler verifying the size is within the limit and it also keeps resulting in issues.


  2. Try increasing the upload_max_filesize and post_max_size in your php.ini file and see if it resolves your issue.

    Here’s more details: https://www.php.net/manual/en/ini.core.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search