文件上传到Slim框架使用curl在PHP [英] File Upload to Slim Framework using curl in php

查看:595
本文介绍了文件上传到Slim框架使用curl在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

无法使用curl在heroku php应用程序中上传文件



b
$ b

在php中处理文件上传后,我试图使用curl发送到我的rest api使用Slim框架。但是,$ _FILES在达到Slim中的函数时总是为空。

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

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

在Slim中接收请求的函数:

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.

推荐答案

据我所知,你需要使用更多的选项,用curl文件上传。请参见此处

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

查看 CURLOPT_UPLOAD 选项和 CURLOPT_POSTFIELDS 的说明,说明您需要在文件名称前使用@以上传(并使用完整路径) 。

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

这篇关于文件上传到Slim框架使用curl在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆