PHP Problem : filesize() return 0 with file containing few data?
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 g开发者_运维百科et the size in bit ? or another solution available?
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);
Which function do you use ?
Because exec() can directly assign result to a variable, so maybe there's no need to save output to a file, if you just want to load it in PHP.
You say:
I use PHP to call a Java command then forward its result into a file called result.txt.
Who does the result writing?
1.The JAVA programm?
2.Do you catch the output in PHP and write it to the file.
3.Do you just redirect the output from command line?
If 1 and 3 you might have a delay between when the result is written in the file so, practically, when you read the file in PHP, if you don't wait for the execution to finish, you read it befor it even was written with the result.
精彩评论