-1

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.

WaXxX333
  • 388
  • 1
  • 2
  • 11
  • I'm sure it's not doing _'absolutely nothing_'. Is there anything in the server error log? – Tangentially Perpendicular Oct 30 '22 at 06:33
  • Nothing. But why are you sure ? Where am I wrong ? It's working on literally every other check I have it doing. There's not even any errors on the page, just a blank page. Is it the nested `if` `else` statements ? I'm lost. I'm literally brand new to PHP. Double checked the logs and there's nothing pertaining to this. – WaXxX333 Oct 30 '22 at 06:37
  • If you're just getting a blank screen PHP has failed in some way. It will log details in the server error log, even if you think it isn't. Check the contents of PHP.INI to find out where it's logging errors, and make sure it can. PHP error logging can be finicky in a Windows environment. – Tangentially Perpendicular Oct 30 '22 at 07:51
  • @TangentiallyPerpendicular, thanks, I solved it. In my `form` I had a size limit that I matched with the handler and that broke it for some reason. By increasing the size by 1 byte in my `form` it resolved the issue. There were no errors in the logs either. I fixed it by accident. – WaXxX333 Oct 30 '22 at 18:43

2 Answers2

0

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

Akhilesh B Chandran
  • 6,523
  • 7
  • 28
  • 55
  • It isn't this. The file size limit was working until I added the if statement for `mime_content_type` to verify the user is uploading an image and turned the actual upload block into a function. I should've added that to my OP. Thank you though. – WaXxX333 Oct 30 '22 at 06:53
  • Duplicate answer ? https://stackoverflow.com/questions/7754133/php-post-max-size-overrides-upload-max-filesize – OMi Shah Oct 30 '22 at 06:56
  • @WaXxX333 did you tried echoing the mime type of the file you uploaded and see if it is one among the allowed list of yours? – Akhilesh B Chandran Oct 30 '22 at 07:03
  • @OMiShah, no, not duplicate. One of a kind as far as I've seen. The file size limit was working until I turned the block of code that's in `proceed()` into the function. Now it isn't doing anything at all. @AkhileshBChandran, yes, it's definitely in the file size limiter. I've been reusing the same few images for testing the whole time. – WaXxX333 Oct 30 '22 at 07:08
  • @WaXxX333 sorry, am a bit confused here. Your code contains an IF block where you check whether the size of the file is above `3145728`. If so, your code would redirect to `index.php` page. Is that what you are talking about? That code block will not finish the uploading process. – Akhilesh B Chandran Oct 30 '22 at 07:13
  • @AkhileshBChandran, I solved it. In my `form`, I set the file size limit to `3145728` as well as in the handler script. Changing that size to 1 byte more in my `form` resolved it. Thank you so much for the help, you led me in the right direction. – WaXxX333 Oct 30 '22 at 07:16
0

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.

WaXxX333
  • 388
  • 1
  • 2
  • 11