简体   繁体   中英

PHP Get File Contents

I'm getting an error that I can't figure out, what I'm doing is I have a string or a bunch of urls and I'm putting them into an array and then looping the array and resizing and uploading the images,but Im getting this error. Any help would be very much appreciated!

ERROR

file_get_contents(http://a513.phobos.apple.com/us/r1000/094/Purple/v4/d4/e4/02/d4e402a3-a485-4d4c-cf9b-90b0af391626/mzl.wbbwbbab.png ) 
[function.file-get-contents]: failed to open stream: HTTP request failed! 
HTTP/1.0 400 Bad Request in "My File" on line 53

PHP

include("picture-resize.php");

    $image = $_POST['thumbnail'];
    $slug = $_POST['slug'];
    $images = $_POST['screenshots'];
    $list = explode(",", $images);      
    $listlength = count($list);

    $i = 0;

    $image = $_POST['thumbnail'];

    $path = parse_url($image, PHP_URL_PATH);

    $filename = $slug.'-'.$i;

    $extension = pathinfo($path, PATHINFO_EXTENSION);

    $file = $filename.'.'.$extension;

    file_put_contents('../tmp/' . $file, file_get_contents($image));

    $picture = new pic_resize();

    $picture->load('../tmp/'.$file);

    $picture->resizeToWidth(125);

    $picture->save('../images/125x125/'.$file, $picture->image_type);

    unlink('../tmp/'.$file);

    $thumbnail = $file;

    $new_list = array();

    while($listlength > $i) {

        $path = parse_url($list[$i], PHP_URL_PATH);

        $extension = pathinfo($path, PATHINFO_EXTENSION);

        $file = $filename.'.'.$extension;

        file_put_contents('../tmp/' . $file, file_get_contents($list[$i]));

        $picture = new pic_resize();

        $picture->load('../tmp/'.$file);

        $picture->resizeToWidth(640);

        $picture->save('../images/640x320/'.$file, $picture->image_type);

        $picture->resizeToWidth(310);

        $picture->save('../images/310x205/'.$file, $picture->image_type);

        unlink('../tmp/'.$file);

        array_push($new_list, $file);

        $i++;
    }
    $screenshots = implode($new_list, ',');
$a = file_get_contents("http://a513.phobos.apple.com/us/r1000/094/Purple/v4/d4/e4/02/d4e402a3-a485-4d4c-cf9b-90b0af391626/mzl.wbbwbbab.png");
echo strlen($a);

works good for me. It can be error on your environment (firewall, wiruses, HTTP request modifications by ISP etc.). Try to paste this link in your browser:

http://a513.phobos.apple.com/us/r1000/094/Purple/v4/d4/e4/02/d4e402a3-a485-4d4c-cf9b-90b0af391626/mzl.wbbwbbab.png

Do you see image or error? You can try other tools to download image, like wget command on unix ot php cUrl . Note that this is no error from code - this ia server response with HTTP code 400 - bad request, so you are connected to image server but HTTP GET request produced by file_get_contents isn't valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM