1

我正在尝试使用 Api 在服务器上上传文件。它在 localhost 上成功,它工作正常,我可以轻松地将文件上传到服务器上。但是当我的代码进入服务器时,它不会被上传。我将读写权限授予我从中获取图像或文件的文件夹。这是我的卷曲代码:

 $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        if($api_type == 2){
            //$mm = array("message" => $message);

            $message = str_replace("\n", "\\n", $message);
            $message = '{"message":"'.$message.'"}';

            curl_setopt($c, CURLOPT_HTTPHEADER, array(
             'Content-Type: application/json',
            ));
            curl_setopt($c, CURLOPT_POST, 1);
            curl_setopt($c, CURLOPT_POSTFIELDS,$message);
        }
        if($api_type == 3 & $attachment != NULL){
            $data = array( 'file' => '@'.self::basePath().'/../user_uploads/hipchat_image/'.$attachment);
            curl_setopt($c, CURLOPT_HTTPHEADER, array(
            'Content-Type: multipart/related',
            ));
            curl_setopt($c, CURLOPT_POST, 1);
            curl_setopt($c, CURLOPT_POSTFIELDS,$data);
        }
        return curl_exec($c); 
4

2 回答 2

3

你可以试试...

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

根据 PHP 版本,这false在 php 5.5 和true5.6+ 中默认为...所以请检查 PHP 的本地和网络服务器版本。

见: http: //php.net/manual/en/function.curl-setopt.php

于 2016-02-11T12:15:49.133 回答
1

PHP 7 删除了这个选项curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);,该CURLFile接口必须用于上传文件。

你可以写这样的代码

$data = array('file'=>new CURLFile('/file/path'));

http://php.net/manual/en/function.curl-setopt.php

于 2017-03-02T10:43:45.717 回答