1

I uploaded some files to a folder, and when I use filesize function I get this message:

Message: filesize() [function.filesize]: stat failed for http://www.books.tahasoft.com/216.ppt

the path of file is:

http://www.books.tahasoft.com/216.ppt

and here is my code:

<?
echo filesize("http://www.books.tahasoft.com/216.ppt");
?>

How can I fix this?

Omid Kamangar
  • 5,768
  • 9
  • 40
  • 69
Zuhair Taha
  • 2,808
  • 2
  • 35
  • 33
  • can you open the file manually? – Zane Oct 23 '12 at 15:10
  • 2
    filesize (and by extension, stat) isn't reliable when used on URLs. if that code is running on the same server as the url is pointing to, then use the local path, e.g. `filesize('/path/to/file/on/the/server/216.ppt')`. – Marc B Oct 23 '12 at 15:11
  • 1
    @MarcB: Actually, it's documented to not work. – Jon Oct 23 '12 at 15:15

6 Answers6

6

You cannot get the filesize over the http protocol.

If the file is on the local server, use filesize with a absolute or a relative path in the filesystem.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
3

You can use like this.......

 $ch = curl_init('http://www.books.tahasoft.com/216.ppt');
 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);
 echo $size;
Avinash
  • 137
  • 8
1

The http:// stream wrapper does not support the stat family of functions, so you can't do this with filesize as the documentation warns:

As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

If you need the filesize over HTTP without having to download the whole file first, you might be able to get it using curl; you can see how to do that here. But keep in mind that this is dependent on what headers the remote server decides to send back; there is no guarantee that it will always work.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
1

You could do this instead.

echo strlen(file_get_contents("http://www.books.tahasoft.com/216.ppt"));
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • wow, downloading a possible very huge file, just to get the filesize? (I did not downvote) – JvdBerg Oct 23 '12 at 15:13
  • -1 because `file_get_contents` over the network to get the filesize? – Jon Oct 23 '12 at 15:13
  • @Jon But if you want to get the size, you need to download it for guarantee, right? – xdazz Oct 23 '12 at 15:17
  • @xdazz: If I want to see if a store is open I need to go over there for a guarantee -- but I can sure *try* calling them first. Also, IMHO it is not very nice to offer such a solution to a newbie without mentioning any alternatives and/or the (huge!) drawbacks. – Jon Oct 23 '12 at 15:19
0

The best you can do, to perform a HTTP HEAD request to the file, and check existence of the Content-Length header; the only other way would be to download the whole file, and check its length - but in my opinion is not a real option.

pozs
  • 34,608
  • 5
  • 57
  • 63
0

for getting the size of your file from filesize() function, you need to add folder path of the file to get the size.

==> False Method: $filePath = "http://www.books.tahasoft.com/216.ppt";

==> True Method: folder path like: $filePath = '/home/www/uploadpath/filename';

So, you get the result by calling the function like:

filesize($filePath);

OR

If you want to get the size from the link, you have to do like this:

strlen(file_get_contents("http://www.books.tahasoft.com/216.ppt"));

Nirav Thakar
  • 166
  • 1
  • 10