0

I'm trying to upload a file using Slim 2. But I'm lost for how I can access the $_FILES[] parameter.

I have the following AJAX call to index.php from JQuery.

 $("form[name='photo_upload']").on('submit',(function() {
    var location='{{site.uri.image}}/profile/{{bio.user_id}}';
    var fd=new FormData(this);
    fd.append('id',{{bio.user_id}});
    fd.append('loc',location);
    $.ajax({
        'url': '{{site.uri.public}}/bio?id={{user.id}}&action=update',
        'type': "POST",
        'data': fd,                
        'cache': false,
        'processData': false,
        'success': function(data){
            var new_loc='{{site.uri.image}}/profile/'+data['photo_loc'];
            $("#frame").attr("src",new_loc);
            $('.editbox.'+cls).addClass("hidden");
        }
    });
}));

Now I pickup the request with the following function in index.php using Slim 2.

$app->post('/bio/?', function () use ($app) {    

        $id=$app->request->params('id');
        $loc=$app->request->params('loc');
        $file = $_FILES['image'];
        $controller = new TE\UserController($app);
        $controller->updateUserPhoto($id, $file, $loc);
        $ret=$controller->getUserBio($id, 'photo');
        error_log($ret);
        echo json_encode(array('photo_loc' => $ret));      
});

and get the following error

Undefined index: image

at the line containing

$file = $_FILES['image'];

on the above code.

I would really appreciate some help on this.

Edit

I get the following when I var_dump[$_FILES].

array(1) { ["image"]=> array(5) { ["name"]=> string(8) "1a_1.PNG" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(24) "C:\xampp\tmp\php585C.tmp" ["error"]=> int(0) ["size"]=> int(19762) } }

It seems $_FILES['image'] and all the file info are there.

I even did an

isset($_FILES['image'])

to check and even that returned true.

So why cant I retrieve if its already there?

samurdhilbk
  • 419
  • 1
  • 5
  • 16
  • var_dump you $_FILES – madalinivascu Sep 20 '16 at 05:21
  • array(1) { ["image"]=> array(5) { ["name"]=> string(8) "1a_1.PNG" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(24) "C:\xampp\tmp\php585C.tmp" ["error"]=> int(0) ["size"]=> int(19762) } } – samurdhilbk Sep 20 '16 at 05:42
  • Above is the var_dump. It seems $_FILES['image'] does exist. So why can't I access it? – samurdhilbk Sep 20 '16 at 05:43
  • how do you want to access it, i don't see you moving the temp file to your photo directory, you should also save a url in a db or something to easily display it on the page – madalinivascu Sep 20 '16 at 05:45
  • I do that by passing the $_FILES['image'] to the $controller object's updateUserBio() method, as you can see in the code. I just want to save $_FILES['image'] to the $file variable so I can pass that data to the php containing the UserController class(of which $controller is an instance). – samurdhilbk Sep 20 '16 at 05:48
  • On what position in the code did the var_dump gave the posted output? – Koen. Sep 20 '16 at 06:41
  • I put it just before the $file = $_FILES['image']; line. – samurdhilbk Sep 20 '16 at 07:18

1 Answers1

-1
<pre>
//Jquery code to upload file 
var data = new FormData();
jQuery.each(jQuery('#file')[0].files, function(i, file) {
    data.append('file-'+i, file);
});

You may now retrieve the file in PHP / Slim PHP Framework using:

$_FILES['file-0']


</pre>

Ref: Sending multipart/formdata with jQuery.ajax

Community
  • 1
  • 1
Senthil
  • 2,156
  • 1
  • 14
  • 19