Search code examples
phpcopyfopenfile-get-contentsfile-put-contents

PHP saves broken image files downloaded from a URL


No matter which function I'm using:

copy("http:" . $imglink, "images/" . substr($imglink, 34));
//or
file_put_contents("images/" . substr($imglink, 34), file_get_contents("http:" . $imglink));
//or
file_put_contents("images/" . $productData['imagefile'], fopen($productData['imagelink'], 'r'));

the files are saved broken and almost 4 times bigger. No errors in the log, already checked that I can manually download healthy images from the remote server through the browser. Any ideas?


Solution

  • Found the problem - the image filename contains space char which should be rawurlencode($imglink)-ed before it is passed as argument