0

I am unable to successfully upload an image/file to my server. The php is as follows:

//This is the directory where images will be saved
$uploadDir = "./";
$uploadFile = $uploadDir . basename( $_FILES['photo']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadFile)){
    echo "The file has been uploaded successfully.";
} else {
    print_r($_FILES);
}

I chose the directory at which this script lives, to ensure the functionality before I upload to the final directory. I want to upload photo's, and will check for file extensions later - but for now I at least need the upload functionality to work.

I get an empty array returned.

** EDIT ** Also, enctype="multipart/form-data" is enabled on the form, and I am submitting it via AJAX.

The form is as follows:

<form id="imageUploadForm" name="imageForm" enctype="multipart/form-data">
    <label for="photo" class="blogLabel">Upload an Image</label>
    <input type="file" name="photo" id="imageUpload" onChange="uploadImage();">
</form>

I do realize that I shouldn't use "onChange" to submit the form.

The AJAX to submit the form is as follows:

function uploadImage() {
    $.ajax({
        type:'POST', 
        url:'imageController.php', 
        data:$('#imageUploadForm').serialize(),
        success: function(responseSubmit) {
                alert(responseSubmit);
            }
     });
};
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
mdance
  • 966
  • 5
  • 16
  • 36
  • 6
    common error is to leave "enctype="multipart/form-data"" off the form. http://www.php.net/manual/en/features.file-upload.post-method.php –  Aug 02 '12 at 20:43
  • That is indeed enabled - Sorry, should have made a note of that. I will edit the question to include that. – mdance Aug 02 '12 at 20:45
  • post the whole form, if $_FILES is empty that's where the problem will be –  Aug 02 '12 at 20:46
  • Can you show what your form looks like? – Naftali Aug 02 '12 at 20:47
  • What you are doing right now is very dangerous. Do not put files within your web root. Do not leave files named the same as what the user specified. Pick a random name, or something the hash of the file, with no file extension. You don't want someone uploading something_evil.php and remotely controlling your server with it! You can recover the original file names later when you serve up those files via a script. – Brad Aug 02 '12 at 20:48
  • you cant do a file upload via ajax. iframe is the only option with out a page refresh. –  Aug 02 '12 at 20:50
  • The files won't be uploaded to the actual web root - I just need to confirm the functionality... at which point I will secure it all. – mdance Aug 02 '12 at 20:50
  • No file upload via AJAX eh? Fair enough - I was not aware of that. – mdance Aug 02 '12 at 20:50
  • possible duplicate of [jQuery Ajax File Upload](http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload) –  Aug 02 '12 at 20:58
  • @mdance DONT CHANGE YOUR WHOLE QUESTION. MAKE A NEW QUESTION PLEASE. I rolled back your edit. – Naftali Aug 02 '12 at 21:01

1 Answers1

4

You cannot upload a file via ajax like that.

This will result in no file being uploaded at all!

You can only do a regular form submit to submit a file to the server in the way you want to do it.


As shown in the comments -- you can do a hidden iframe trick to submit the files to the server without using ajax.


Or you can use XHR2

Naftali
  • 144,921
  • 39
  • 244
  • 303