2

我正在尝试通过 curl 在另一台服务器上上传文件。我为此创建了一个脚本,但我无法获取$_FILES参数。它是空的。

$request = curl_init('http://localhost/pushUploadedFile.php');
$file_path = $path.$name;
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
     $request,
     CURLOPT_POSTFIELDS,
     array(
      'file' => '@' . $file_path,
      'test' => 'rahul'
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);exit();

pushUploadedFile.php:

print_r($_FILES['file']);
4

3 回答 3

2

您使用的是什么版本的 PHP?在 PHP 5.5CURLOPT_SAFE_UPLOAD中引入了 curl 选项,该选项true从 PHP 5.6.0 开始默认为 startet。当它是true文件上传使用@/path/to/file被禁用。因此,如果您使用的是 PHP 5.6 或更新版本,则必须将其设置false为允许上传:

curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);

但是从@/path/to/filePHP 5.5.0 开始,上传的格式已经过时并且不推荐使用,您CurlFile现在应该使用该类:

$request = curl_init();
$file_path = $path.$name;
curl_setopt($request, CURLOPT_URL, 'http://localhost/pushUploadedFile.php');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
     $request,
     CURLOPT_POSTFIELDS,
     array(
      'file' => new CurlFile( $file_path ),
      'test' => 'rahul'
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
于 2015-12-14T12:56:06.870 回答
2
$file_name_with_full_path = realpath('./sample.jpeg');
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
于 2015-12-14T11:43:25.690 回答
0
        $target_url ="http://www.localwork.com/pushUploadedFile.php";     
        $file_full_path = $path.$img_name;            
        $file_name_with_full_path = new CurlFile($file_full_path, 'image/png', $name);

        $post = array('path' => $path,'file_contents'=>$file_name_with_full_path);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$target_url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $result=curl_exec ($ch);
        curl_close ($ch);
于 2015-12-14T13:58:29.523 回答