0

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;
        }

    }
}
DACrosby
  • 11,116
  • 3
  • 39
  • 51
Dexvi
  • 9
  • 2

4 Answers4

2

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

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • HTTP has sizing data: Content-Length – hek2mgl Aug 02 '13 at 16:05
  • Yes, but it's not used by the HTTP wrapper. Content-length is frequently wrong, especially on dynamically built data. – Marc B Aug 02 '13 at 16:06
  • First it looked like a missing feature for me, but `Content-length is frequently wrong, ... ` might be the reason for not implementing this. however, your answer is correct: `filesize()` supports some protocol wrappers, but not http.. – hek2mgl Aug 02 '13 at 16:10
2

you can try this instead

$imgbinary = file_get_contents($img_src);
Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
0

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.

oldmonk
  • 739
  • 4
  • 10
0

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!

Community
  • 1
  • 1
  • In this case it is not necessary to first do a http request to acquire the file size and then perform another one to get the data. – urzeit Aug 02 '13 at 16:30