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); } }); }; 
10
  • 6
    common error is to leave "enctype="multipart/form-data"" off the form. php.net/manual/en/features.file-upload.post-method.php Commented Aug 2, 2012 at 20:43
  • That is indeed enabled - Sorry, should have made a note of that. I will edit the question to include that. Commented Aug 2, 2012 at 20:45
  • post the whole form, if $_FILES is empty that's where the problem will be Commented Aug 2, 2012 at 20:46
  • Can you show what your form looks like? Commented Aug 2, 2012 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. Commented Aug 2, 2012 at 20:48

1 Answer 1

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

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

8 Comments

@Dagon haha I just loaded that comment :-P Great minds think alike ^_^
Yes, Thank you :) I was not aware of that.
@Dagon we were about 15 seconds apart in our posting :-P
still can do the iframe "trick" will result in no full page reload - if that is what the op wanted
@Dagon true. I added that to the answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.