[英]File uploading through curl in php
我正在尝试通过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']);
您正在使用哪个版本的PHP? 在PHP 5.5中,引入了curl选项CURLOPT_SAFE_UPLOAD
,该选项从PHP 5.6.0开始默认为true
。 当它是true
文件上传使用@/path/to/file
被禁用。 因此,如果您使用的是PHP 5.6或更高版本,则必须将其设置为false
以允许上传:
curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);
但是自PHP 5.5.0起,用于上传的@/path/to/file
格式已过时和不赞成使用,您现在应该使用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);
$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);
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.