在php中通过curl上传文件 [英] File uploading through curl in php

查看:88
本文介绍了在php中通过curl上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to upload a file through curl on another server. I have created a script for this, but I am not able to get the $_FILES parameter. It's empty.

$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:

pushUploadedFile.php:

print_r($_FILES['file']);

推荐答案

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

What version of PHP are you using? In PHP 5.5 the curl option CURLOPT_SAFE_UPLOAD was introduced which startet defaulting to true as of PHP 5.6.0. When it is true file uploads using @/path/to/file are disabled. So, if you are using PHP 5.6 or newer you have to set it to false to allow the upload:

curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);

但是上传的 @/path/to/file 格式已经过时并且从 PHP 5.5.0 开始被弃用,您应该使用 CurlFile 现在这个类:

But the @/path/to/file format for uploads is outdated and deprecated as of PHP 5.5.0, you should use the CurlFile class for this now:

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

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

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