0

I'm trying to save a jpg file from a dynamic url, looks like this,

http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api

file_get_contents can't get the content correctly, here is my code,

<?php
$image_url = "http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api";
$img =  file_get_contents($image_url);
$folder = 'C:/xampp/htdocs/test/test.jpg';
file_put_contents($folder, file_get_contents($img));
?>

Appreciate any ideas or alternative as "easy" methods.

1 Answer 1

2

One problem is that you have 2 file_get_contents calls. The first call:

$img =  file_get_contents($image_url);

Returns the response from the request to the URL and stores it in the $img variable. The second call:

file_put_contents($folder, file_get_contents($img));

Doesn't make any sense. Instead, just do this:

file_put_contents($folder, $img );
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, How stupid of me :) Thank you, I wasted 45 minutes on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.