2

Possible Duplicate:
Failing to upload file with curl in heroku php app

After handling a file upload in php, I am trying to send that file using curl to my rest api which uses Slim Framework. However, $_FILES is always empty once it reaches the function in Slim.

sender.php

if(move_uploaded_file($_FILES['myFile']["tmp_name"], $UploadDirectory . $_FILES['myFile']["name"] ))
{
    $ch = curl_init();
    $data = array('name' => 'test', 'file' => $UploadDirectory . $_FILES['myFile']["name"]);

    curl_setopt($ch, CURLOPT_URL, 'http://localhost/slimlocation/upload/');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_exec($ch); 
 }

And the function to receive the request in Slim:

$app->post('/upload/', function() use ($app) {
    if(isset($_FILES)){

        // count is always zero
        echo count($_FILES);

    }
});

Am I sending the file incorrectly and / or is it possible to do what I am attempting? Any help is appreciated.

Community
  • 1
  • 1
Drew
  • 2,601
  • 6
  • 42
  • 65

2 Answers2

1

As far as i know, you need to use more options for a file upload with curl. See here.

Look at the CURLOPT_UPLOAD option and the description of CURLOPT_POSTFIELDS, it says that you need to use an @ before the file name to upload (and use a full path).

Eduardo Reveles
  • 2,155
  • 17
  • 14
  • 1
    Thanks so much. I did not need CURLOPT_UPLOAD, but the file name / full path was my issue. – Drew Nov 01 '12 at 20:11
0

These were the changes I needed and it worked:

$filepath = str_replace('\\', '/', realpath(dirname(__FILE__))) . "/";
$target_path = $filepath . $UploadDirectory . $FileName;
$data = array('name' => 'test', 'file' => '@'.$target_path);
Drew
  • 2,601
  • 6
  • 42
  • 65