从php中的url上传图像时出错(&;wordpress)


Error upload image from url in php (&wordpress)

我为wordpress开发了一个插件,可以从URL下载图片来创建帖子(文章)。

在大多数情况下,图片是下载和发布文章的,但有时,出于奇怪的原因,图片没有下载。

具体来说,损坏的文件是在没有声明错误的情况下下载的。(类似于下载被剪切等待过程)

具体示例:http://static.lexpress.fr/medias_10713/w_1624,h_1219,c_crop,x_345,y_113/w_605,h_350,c_fill,g_north/v145339163/marine-le-pen-france-national-front-political party-head-checks-notes-on-her-mobile-phone-after-leaving-her-politing station-during-european-palliament-election-in-henn-beaumont_5485394.jpg

看看这个链接。你可以用浏览器看到这张图片,没有问题。但是我的插件无法下载这个文件。您可以使用此网站进行测试:img上传器尝试将此图像与此网站一起上载,您将出现错误。但是为什么?!?图像可以在浏览器中打开!我疯了,你知道吗?我不知道我能在网上搜索什么来找到解决方案。。

感谢

使用curl,我下载这个图像没有任何问题:

$ch = curl_init('http://static.lexpress.fr/medias_10713/w_1624,h_1219,c_crop,x_345,y_113/w_605,h_350,c_fill,g_north/v1450339163/marine-le-pen-france-s-national-front-political-party-head-checks-notes-on-her-mobile-phone-after-leaving-her-polling-station-during-the-european-parliament-election-in-henin-beaumont_5485394.jpg');
$fp = fopen('./test.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

我猜您使用的是file_get_contents。通过curl下载图片二进制似乎绕过了限制。

非常感谢Vard,它运行得很好!

但我给其他人的信息:有些服务器没有激活CURL,所以你必须在使用这个黑客之前检查它。

检查代码:

  if (!is_callable('curl_init')) {
        error_log("Curl no exist, request impossible..", 3, plugin_dir_path(__FILE__)."../logs/error.log");
        header("HTTP/1.0 501 Not Implemented");
        exit("Curl request impossible for wordpress server");
    }
enter code here

Grand Merci Vard(对不起,我没有足够的理由对你的回应投赞成票,但你很友善)