PHP 5.6< / cURL帖子文件

时间:2015-12-11 14:58:47

标签: php curl

如何从我的网站(服务器)发布文件(路径 / var / www / httpdocs / domain.com /images/image.jpg )到另一个网站?

我试过了:

$ch = curl_init();
$Url = "http://domain.com/insert.php";
$cookie_file = 'cookies.txt';
$file_name_with_full_path = realpath('/images/image.jpg');
$fields = array(
            'type' => urlencode(1),
            'category' => urlencode(1),
            'title' => "Some title",
            'description' => "Some description",
            'file' => '@'.$file_name_with_full_path
);
$fieldsTrim = http_build_query($fields);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsTrim);
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath($cookie_file));
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($cookie_file));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);

但它不起作用..

它发送除文件...

之外的所有内容

由于

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的代码中:curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 假设您的insert.php与您尝试将文件上传到的服务器位于同一服务器上,这应该可行。否则,对于PHP v5.6,您可以尝试curl_file_create()。一个很好的教程是here,用于检查curl_file_create(),如果没有,则会回溯到较旧的上传文件的方式。

另请参阅:cURL file uploads not working anymore after upgrade from PHP 5.5 to 5.6