0

so here's the problem. I am getting some strange behavior from my php script that I use to upload images without refreshing a site. Bellow there are 2 scripts, the jquery / ajax and the php. And actually it works great but i found an error that occurs. This is what happens.

I choose an image that is less than 5 MB - image uploads - correct I choose an image of 7 MB - i get an error "image is to big" - correct I choose an image that is less than 5 MB - image uploads - correct I choose an image (the same one) of 7 MB - i get "invalid argument supplied for foreach" and "not an image"

This happens when i choose images exactly in that order. I tried choosing images less than 5mb an every time it's ok. I don't know what's going on. Have in mind that I am a begginer in PHP so the code probably isn't "as should be" and I'm opened for suggestions on what to improve or do differently

html

<form class="uploadImageForm" method="post" enctype="multipart/form-data">
<input type="file" name="userImage" id="images">
<input type="submit" name="submitImage">  
</form>

Jquery / ajax code

if (window.FormData) {
    formdata = new FormData();
    $('#images').on("change", function(evt) {

        var reader, file;   
        file = this.files[0];

        if (window.FileReader) {
            reader = new FileReader();
            reader.readAsDataURL(file);
        }

        if (formdata) {
            formdata.append("userImage[]", file);
        }

        if (formdata) {
            $.ajax({
            url: "includes/upload.php",
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false,
            success: function (res) {

                var result = res;
                if(result == "not an image"){
                    console.log("error - this is not an image");
                }else if(result == "image too big"){
                    console.log("error - image to big")
                }else{
                    console.log("result", result);
                    $('img.userImage').attr("src", result);
                }
            }
            });     
        }   

    });
}else{
    $('input[name="userImage"]').on("change", function(){
        $('input[name="submitImage"]').trigger("click");
    });
}  

PHP CODE

<?php
session_start();
$sessionId = session_id();

foreach ($_FILES["userImage"]["name"] as $key => $value) {
    $name = $_FILES["userImage"]["name"][$key];
    $size = $_FILES["userImage"]["size"][$key];
    $tmpName = $_FILES["userImage"]["tmp_name"][$key];
    $type = explode("/", $_FILES["userImage"]["type"][$key]);
    $type = $type[1];
}
$possibleTypes = array("jpeg", "jpg", "gif", "png", "JPEG", "JPG");
$maxSize = 5000000;

if($size > $maxSize){
    echo "image too big";
    die();
}
else if(!in_array($type, $possibleTypes)){
    echo "not an image, this type is: " .$type;
    die();
}
else{
    $directory = ("../user_img/".$sessionId);
    if(!file_exists($directory)){
        mkdir($directory, 0777);
    }
    else{
        foreach(glob($directory ."/*.*") as $file) {
            unlink($file);
        }
    }
    move_uploaded_file( $tmpName, $directory."/" . $sessionId."_img.".$type);   

$img_src =  "user_img/".$sessionId."/".$sessionId."_img.".$type;
echo $img_src;
}
?>
2
  • 1
    chmod 777 is very very bad. Commented Mar 3, 2014 at 9:12
  • what should i use then? Commented Mar 3, 2014 at 9:14

1 Answer 1

1

You have to increase the upload_max_filesize and post_max_size values in your php.ini.

It seems like the provided image is too big for uploading and so you have no image uploaded, e.g. noh values you can loop over in a foreach.

EDIT

Regarding your comments i think i have found the error:

You instantiate formdata = new FormData(); before the onChange-Handler. So after the input changed you append the img-path to the array. And after you've uploaded the image you add the next one. But the formData-Array also contains the previous one and is not cleared. So while you are uploading new images without a page refresh it adds up and all previous uploaded images are transferred too. Everytime.

Sign up to request clarification or add additional context in comments.

7 Comments

And you restarted your apache? What is echo php_ini_get('upload_max_filesize'); printing?
i didn't restart apache, the values where set to 15 mb before you event posted the comment.. restared the apache. still it says 15 mb as it should
i just figered out something thanks to you. Even though the options are correctly set, they are causing the problem, but in a strange way. Every time i choose a different picture, it's size get's added up to the previous image size, and when that goes beyond the limit in php.ini (e.g. 15mb) it throws me an error. Do You know how can i fix that?
Hm that sounds indeed strange. When will the request be send? After you selected one image or after you click an upload button, so multiple selection is possible?
i edited the javascript code above. on line 3 is the .on("change") which triggers the ajax... multiple selection isn't possible.. only one image at a time
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.