Reputation: 9
My script always worked fine on localhost. Now I've moved all my images on another website and the script won't work anymore. Why is this, and how can I fix it?
Error:
Warning: filesize(): stat failed for http://data.localsky.net/panel/img/blocked/box_main.gif in C:\xampp\htdocs\Projects\MVC\application\class.security.php on line 15
I call the function with:
baseImg('http://data.localsky.net/panel/img/blocked/box_main.gif', false);
public function baseImg($path, $auto=true) {
$img_src = $path;
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);
if ( preg_match('/(?i)msie [1-8]/', $_SERVER['HTTP_USER_AGENT']) ) {
if($auto) {
return '<img src="'.$img_scr.'" />';
} else {
echo $img_src;
}
} else {
if($auto) {
return '<img src="data:image/jpg;base64,'.$img_str.'" />';
} else {
return 'data:image/jpg;base64,'.$img_str;
}
}
}
Upvotes: 0
Views: 867
Reputation:
As MarcB pointed out, you can't use filesize()
on a remote file. Here's a cURL based solution, I found here:
Code:
function retrieve_remote_file_size($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
return $size;
}
Usage:
echo retrieve_remote_file_size('http://data.localsky.net/panel/img/blocked/box_main.gif');
Output:
53
Hope this helps!
Upvotes: 0
Reputation: 739
filesize only can be used for local file. If you want get file size of remote file, plz use below code:
<?php
$remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}
echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>
Code is copied from http://php.net/manual/en/function.filesize.php.
Upvotes: 0
Reputation: 6230
you can try this instead
$imgbinary = file_get_contents($img_src);
Upvotes: 2
Reputation: 360562
You cannot use filesize() on HTTP URLs. Not all protocols provide sizing data, or support fetching it. filesize() should be used on LOCAL files only. The supported protocols are listed in the functions' man page: http://php.net/filesize and http://php.net/manual/en/wrappers.http.php
Upvotes: 2