1

What I'm trying to do

I have a form which is posted via ajaxForm. The form contains a file input field, however the data isn't being processed with the rest of the information in the POST.

The code

HTML Form

<form id="profilepicForm" action="user/profilepic.php" method="post" enctype="multipart/form-data">
    <input type="file" accept="image/gif, image/jpeg, image/png" name="file" />
    <input type="hidden" name="userid" value="<?php echo $_SESSION['user']['id'] ?>" />
    <input type="submit" value="Upload">
</form>

Javascript

var options = { 
    complete: function(response) {
        $("#profilepicMessage").html(response.responseText);
    },
    error: function(){
        $("#profilepicMessage").html("ERROR: unable to upload file");
    } 
};

$("#profilepicForm").ajaxForm(options);

PHP

$user_id    = $_POST['userid'];
$image      = $_FILES['file']['name'];

print_r($_POST);
exit;

What's happening

All that comes through is Array ( [userid] => 34 ), where 34 is my particular userid. Therefore I know that the form is being posted, but the file is not going through.

Ben
  • 8,894
  • 7
  • 44
  • 80
  • See http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery?rq=1 You need to use HTML5 to do ajax file uploads. You are using the ajaxForm jQuery plugin which probably does not support file uploads. – SamV Sep 10 '13 at 00:02
  • [This website tutorial says otherwise](http://hayageek.com/ajax-file-upload-jquery/), and is apparently fully functional. – Ben Sep 10 '13 at 00:04
  • If you want to make a nice uploader, plupload is a good solution. Worth to give a look. – long.luc Sep 10 '13 at 00:05
  • [look here](http://malsup.com/jquery/form/#code-samples) the plugin page gives examples of file uploads – Liam Allan Sep 10 '13 at 00:14

2 Answers2

1

You should look at global variable $_FILES.

long.luc
  • 1,191
  • 1
  • 10
  • 30
  • Yes thanks for this. It can see the file and successfully shows the array but it won't upload the file. – Ben Sep 10 '13 at 00:16
  • http://www.php.net/manual/en/function.move-uploaded-file.php -> may some examples could help you in that case. – long.luc Sep 10 '13 at 00:25
-1

You'll have to look for something like uploadifive to manage that, for ajax can't currently handle file transfers

(this is not entirely true, there is ajax2 and html5 file api, but save yourself a problem, look for uploadifive).

ffflabs
  • 17,166
  • 5
  • 51
  • 77