File Upload to Slim Framework using curl in php [duplicate]

混江龙づ霸主 提交于 2019-12-24 01:18:50

问题


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.


回答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).




回答2:


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);


来源:https://stackoverflow.com/questions/13183972/file-upload-to-slim-framework-using-curl-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!