我正在尝试通过另一台服务器上的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']);
答案 0 :(得分:2)
您使用的是哪个版本的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);
但是上传的@/path/to/file
格式已经过时并且在PHP 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);
答案 1 :(得分:1)
// When initialization must be separate from declaration, e.g. class members,
// initialize with nullptr to make your programming intent explicit.
shared_ptr<Song> sp5(nullptr);
//Equivalent to: shared_ptr<Song> sp5;
答案 2 :(得分: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);