Error upload image from url in php (&wordpress)

I develop a plugin for wordpress which download image from URL for create a post(article).

For the most part, the image is download and article posted but sometime, for curious reason, the image is not download.

Read More

Specifically, a file corrupted is download without declared errors. (like download is cutted pending process)

Concrete example :
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

Look this link. You can see this image with your browser without problem.
But my plugin cannt download this file. You can test that with this site :
img uploader
Try to upload this image with this site and you will have a error. But why ?!? The image can be open in browser! I become crazy, do you have idea please ? I don’t know what can i search on web for find a solution..

Thanks you

Related posts

2 comments

  1. Using curl I didn’t have any issue to download this image:

    $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);
    

    I guess you were using file_get_contents instead. Downloading the picture binary through curl seems to bypass the limitations.

  2. Thanks you so much Vard, it’s working perfeclty!

    But i give informations for other peoples : Some server don’t active CURL, so you must check it before use this hack.

    Code for check that :

      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 (sorry i don’t have enought point for a positive vote about your response but you are so kind)

Comments are closed.