0

I am using an AJAX Form to upload a profile picture on my website through ajax but the thing is that I am not able to do it. I've made a php page for that where the form is going to be posted through ajax. Have a look:

<?php
$file = $_FILES["file"];
$filename = $_FILES["file"]["name"];
$tempdir = $_FILES["file"]["tmp_name"];
$error = $_FILES["file"]["error"];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];

$maxsize = 524288;
$allowedtypes = array("image/png", "image/jpg", "image/jpeg", "image/bmp");

$errormsg = "";
if(!empty($error))
{
    switch($error)
    {
        case '1':
            $errormsg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
            break;
        case '2':
            $errormsg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
            break;
        case '3':
            $errormsg = 'The uploaded file was only partially uploaded';
            break;
        case '4':
            $errormsg = 'No file was uploaded.';
            break;
        case '6':
            $errormsg = 'Missing a temporary folder';
            break;
        case '7':
            $errormsg = 'Failed to write file to disk';
            break;
        case '8':
            $errormsg = 'File upload stopped by extension';
            break;
        default:
            $errormsg = 'No error code avaiable';
    }
} elseif(empty($tmpdir) || $tmpdir == 'none') {
    $errormsg = 'No file was uploaded..';
} elseif(!in_array($type, $allowedtypes) || $size > $maxsize) {
    $errormsg = 'Either image type not supported or size is extending 512 KB';
} else {
    $filename = $_SESSION["username"];
    $filename_array = explode(".",$_POST["filename"]);
    $extension = array_pop($filename_array);
    $path = 'folder/' . Utilities::GetDefaultPicturePath() . $filename . "." . $extension;
    $location = $path . $filename; 
    move_uploaded_file($tmpdir, $location); 
    //for security reason, we force to remove all uploaded file
    @unlink($file);     
}

echo $errormsg;
?>

And here's my AJAX:

<form action="admin/upload_profile_picture.php" method="post" class="hide" enctype="multipart/form-data" id="form">
    <input type="file" id="file" class="hide" name="file" />
    <input type="submit" name="submit" id="submit" />
</form>

And as the form is hidden the submit is called through another button:

<button class="btn btn-primary" onclick='submitForm();'>Save changes</button>

And the file box is opened through:

<button class="btn btn-primary" id="upload">Upload</button>

And the clicks of those two buttons are called through this script:

$("#upload").click(function () {
    $("#file").click();
});

$("#save").click(function () {
    $("#submit").click();
});

And lastly the ajaxForm() jquery plugin script is here:

$('#form').ajaxForm(function (data) {
    console.log(data);
});

As you can see that I am logging the data that is returned. What I always get on submitting the form is that "No file was uploaded..". And as you can see on my PHP page, that error is only printed when temporary directory of the file is either empty of is set to "none".

So what's the problem and how can I resolve the issue.

Thanks.

1
  • 1
    If you look into the network inspector of your browser console, does it show the file being uploaded? Commented Dec 19, 2013 at 9:11

2 Answers 2

5

You assign result to $tempdir in line 4 and check then $tmpdir below

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

4 Comments

Lol, sharp, +1 for spotting that this quick.
Sorry, but I didn't understood. Can you please explain? :)
Variable name is different. Notice that you make assignment $tempdir = $_FILES["file"]["tmp_name"]; and then you check elseif(empty($tmpdir) || $tmpdir == 'none'). $tmpdir is unasigned variable, different than $tempdir
Thanks. It worked. There were some other problems aswell but I cleared them myself. Thanks :)
1

In your code, you are putting move_uploaded_file logic in the last else condition.

if(){
  ............................
}
elseif(empty($tmpdir) || $tmpdir == 'none') {
    $errormsg = 'No file was uploaded..';
} elseif(!in_array($type, $allowedtypes) || $size > $maxsize) {
    $errormsg = 'Either image type not supported or size is extending 512 KB';
} else {
    $filename = $_SESSION["username"];
    $filename_array = explode(".",$_POST["filename"]);
    $extension = array_pop($filename_array);
    $path = 'folder/' . Utilities::GetDefaultPicturePath() . $filename . "." . $extension;
    $location = $path . $filename; 
    move_uploaded_file($tmpdir, $location); 
    //for security reason, we force to remove all uploaded file
    @unlink($file);     
}

Since tmpdir is empty before file move,so control is gone to this block :

  elseif(empty($tmpdir) || $tmpdir == 'none') {
        $errormsg = 'No file was uploaded..';
    } 

Above check should be added after you move file there ideally.

And in last else condition file is moved to tmpdir, so check that directory after filemove You'll find it there.

Comments

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.