Download favicon from url returns broken .ico files
Download favicon from url returns broken .ico files
I tried to create a script to download favicons from a list of links:
My file link_list.txt
looks like the following:
link_list.txt
http://stackoverflow.com
http://google.com
facebook.com
I am using the following library to check and download favicons:
Github: FaviconDownloader
My script currently looks like the following:
<?php
require_once 'vendor/autoload.php';
use VincepareFaviconDownloaderFaviconDownloader;
$fh = fopen(dirname(__FILE__).DIRECTORY_SEPARATOR . 'link_list.txt','r');
while ($line = fgets($fh)) {
//if the url has not http:// add it
if(preg_match("@^http://@i",$line))
$line = preg_replace("@(http://)+@i",'http://',$line);
else
$line = 'http://'.$line;
echo('URL: ' . $line."n");
$favicon = new FaviconDownloader($line);
if (!$favicon->icoExists) {
echo "No favicon for ".$favicon->url;
}
// get name of url
$parts = parse_url($line);
$path_parts = explode('.', isset($parts['host'])?$parts['host']:$parts['path']);
echo 'Filename: fav-'. $path_parts[0] .'.ico' . "n";
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'fav-'. $path_parts[0] . '.ico';
file_put_contents($filename, $favicon->icoData);
echo "Saved to ".$filename."nn";
}
fclose($fh);
When I run my script I only get to download the favicon of facebook, the other favicons - stackoverflow and google - are broken:
Any suggestions what is wrong with my script?
I appreciate your replies!
@lxg Please see the image that I added above. The saved files look like this.
– Anna.Klee
Jun 29 at 11:55
@Anna.Klee find the file on your filesystem, right-click it and open it with plain text editor such as notepad or gedit. It might contain error messages instead of image data.
– ino
Jun 29 at 12:15
@ino I tested the code above now even more and I found that only the last favicon gets downloaded. Any suggestions why this is happening?
– Anna.Klee
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How are they broken? Have you looked inside the files? Maybe they contain error message. What HTTP status and MIME type did the response have?
– lxg
Jun 29 at 11:47