0

I am trying to save some images from different links which are saved in a .txt but I'm getting an error

failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

If I open the url it works and if I paste the url directly into the code it works also. But why doesn't it work if I want to take the url from the txt file?

<?php 
$links = file('download.txt');

foreach ($links as $link) {
    $path = "C:/xampp/htdocs/download/sp/". basename($link);
    $image = file_get_contents($link);
    file_put_contents($path,$image);
}?>
elkloso
  • 27
  • 1
  • 7
  • 1. Do the links contain non-ASCII characters? 2. Can you enable error reporting and display errors (howto: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and see if `file_get_contents` produces some useful message? – ob-ivan Jan 30 '18 at 19:11
  • The links contain only alphanumerical charactes and minus/points. I activated error reporting but I get only the message: Warning: file_get_contents(http://www.myadress.de/image.jpg ): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\xampp\htdocs\download\download.php on line 9 – elkloso Jan 30 '18 at 19:19
  • Try `file('...', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)`. It may be newline characters and empty lines which cause the problem. If this doesn't help, then `var_dump($link)` before `file_get_contents` and see what link it breaks at. – ob-ivan Jan 30 '18 at 20:15

0 Answers0