PHP - Image Upload , move_uploaded_file not working?

238 views Asked by At

I have coded a image upload script but when I click upload i get redirected to the upload page?

Here is the code:

    $image1name = $_FILES['image1']['name'];
    $image1crntloc = $_FILES['image1']['tmp_name'];
    $image1ext = pathinfo($image1name, PATHINFO_EXTENSION);
    $image1size = $_FILES['image1']['size'];

    $allowedext = array("jpg","gif","png");
    //check image 1 extension.
    if (!in_array($image1ext,$allowedext))
    {
        echo "<script>alert(\"Image 1 has an invalid file.\");</script>";
    }

    else{
        $image1final = md5(time($image1name));
        $saveimage1 = "../images/".$image1final.".".$image1ext;
        $image1uploadresult = move_uploaded_file($image1crntloc,       $saveimage1); 

        if ($image1uploadresult == TRUE)
        {
            echo "uploaded.";
        }
        else{
            echo "image not uploaded.";
        }

as soon as i click upload, i get redirected to the page where the user selects the image ,i have also checked the directory and nothing gets uploaded? There is no PHP error shown as well.

Any help?

Thanks.

CODE FOR FORM:

<form id="new-ad" name="new-ad" method="post" action="includes/create.php" enctype="multipart/form-data">
<div class="form-group animated fadeIn">
<label class="labelcustom" for "image1">Image #1:</label>
<br />
Select image to upload:
<input type="file" class="form-control" name="image1" id="image1" />
<input type="submit" name="submit" />
</form>
2

There are 2 answers

0
l'L'l On BEST ANSWER

In your code you are missing the closing bracket on your else statement:

else {
    echo "image not uploaded.";
}

Should be:

    else {
        echo "image not uploaded.";
    }
}
2
AdminXVII On

In your form, you forgot the max file size. Add <input type="hidden" name="MAX_FILE_SIZE" value="2097152" /> to it, were value is the max size in bytes to accept in the form.