0

I am trying to use the filesize php function to automatically write the filesize in a meta tag I am using.

What I am doing normally is for example

<meta name="DC.format" content="<?= filesize($filename); ?> bytes">

Now, if this is used with the following syntax it returns:

php
<meta name="DC.format" content="<?= filesize("index.php"); ?> bytes">

resulting html
<meta name="DC.format" content="6412 bytes">

But if I use the following syntax I get the following error.

php
<meta name="DC.format" content="<?= filesize("index.php?locale=en_US"); ?> bytes">

resulting html
<meta name="DC.format" content="<br /><b>Warning</b>:  filesize():
stat failed for index.php?es=./&amp;en=./en/&amp;locale=en_US in
<b>D:\xampp\htdocs\casasenmeridabaspul.com-v2\header.php</b>
on line <b>87</b><br /> bytes">

What I suppose is happening is that filesize is getting the filesize of the specific file without doing any php processing or such. How can I calculate the resulting file size after all the server side has processed everything?

With a little more detail:

I have header.php, footer.php and index.php where index.php has the previous 2 included. I was thinking perhaps something like curl but I am not really familiar with it so I would leave it to the experts.

Thanks in advance.

Mihail Minkov
  • 2,463
  • 2
  • 24
  • 41

2 Answers2

1

You pass an (invalid) local path to filesize() and it therefore tries to resolve it on your hard disk. This obviously fails due to the query parameters which invalidate the file name.

It is a valid part of a URI however. You can therefore start a HTTP request and get the size of the returned content:

PHP: Remote file size without downloading file

Please bear in mind that this method will fire off a HTTP request which can influence the performance quite a bit.

Community
  • 1
  • 1
ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • Is there a way to calculate it without the HTTP request? – Mihail Minkov Dec 18 '13 at 22:53
  • This one seems more complex than the next one, why would you recommend it? – Mihail Minkov Dec 18 '13 at 23:14
  • @mihail_ov Well, since these are your PHP files and contain your logic, you could calculate the size (fire off an internal request; 'simulate' a request from an extern source internally). I recommend against AbraCadaver's solution because it will always download the file! This has a serious performance impact. The answer in the link I provided only executes a 'HEAD' requests which only returns some metadata. – ComFreek Dec 19 '13 at 15:05
1

It's not a file. It's text output from the execution of the file. The best you can do is strlen() or mb_strlen() without using HTTP functions / classes I think:

<?= strlen(file_get_contents("http://www.example.com/index.php?locale=en_US")); ?>
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • `file_get_contents()` will use a HTTP request. – ComFreek Dec 18 '13 at 21:45
  • @ComFreek: Yes, they are passing a query string. – AbraCadaver Dec 18 '13 at 21:47
  • I think I'm having a cycle issue. As index.php includes header.php and the function is in header.php, when I am asking to calculate the filesize it goes to header again thus repeating the function and never calculating anything. I was thinking perhaps adding an extra $_GET parameter and identifying it. What do you think? – Mihail Minkov Dec 18 '13 at 22:52
  • I did it as I mentioned: ` if(!$_GET["calc"]) { echo strlen(file_get_contents("http://www.site.com/".$filename."?".$_SERVER["QUERY_STRING"]."&calc=1")); } ?>` – Mihail Minkov Dec 18 '13 at 23:13