【问题标题】:PHP 5.6< / cURL post filePHP 5.6</cURL 发布文件
【发布时间】:2016-03-17 12:49:42
【问题描述】:

如何从我的网站(服务器)发布文件(路径 /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);

但它不起作用..

它会发送除文件以外的所有内容...

谢谢

【问题讨论】:

  • 远程服务器中的文件路径是否缺少$_POST['file']$fieldsTrim = http_build_query($fields); $fields 是什么?我在您的代码中没有看到 $fields
  • 对不起,我更新了代码。
  • 所以$_POST['file'] 是空的?还有语法错误`'file' => '@'.$file_name_with_full_path)`)
  • No $_POST['file'] 在我尝试发布图片时不为空。语法错误只是我点击这里。
  • 我模拟你的代码,然后用这个c2n.me/3rBv1yp 一切都好;

标签: php curl


【解决方案1】:

尝试将此添加到您的代码中: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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2015-03-17
    • 1970-01-01
    • 2013-01-14
    • 2015-07-29
    • 2015-12-06
    相关资源
    最近更新 更多