Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Problem : filesize() return 0 with file containing few data?

Tags:

java

php

I use PHP to call a Java command then forward its result into a file called result.txt. For ex, the file contains this: "Result is : 5.0" but the function filesize() returns 0 and when I check by 'ls -l' command it's also 0. Because I decide to print the result to the screen when file size != 0 so nothing is printed. How can I get the size in bit ? or another solution available?

like image 344
Duc Tran Avatar asked Mar 02 '11 12:03

Duc Tran


1 Answers

From the docs, when you call filesize, PHP caches this result in the stat cache.

Have you tried clearing the stat cache?

clearstatcache();

If it does not work, possible workaround is to open the file, seek to its end, and then use ftell.

$fp = fopen($filename, "rb");
fseek($fp, 0, SEEK_END);
$size = ftell($fp);
fclose($fp);

If you are actually planning to display the output to the user, you can instead read the entire file and then strlen.

$data = file_get_contents($filename);
$size = strlen($data);
like image 193
Thai Avatar answered Sep 28 '22 06:09

Thai