PHP Problem: filesize () returns 0 with a file containing multiple data?

I use PHP to invoke the Java command, and then send its result to a file called result.txt. For example, the file contains the following: "Result: 5.0" but the filesize () function returns 0, and when I check with the ls -l command, it is also 0. Because I decide to print the result on the screen with the file size! = 0, so nothing not printed. How can I get the size in bits? or another solution?

+4
source share
3 answers

From docs, when calling filesize PHP caches this result in the stat ket.

Have you tried clearing the stat cache?

 clearstatcache(); 

If this does not work, a workaround is perhaps to open the file, find 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 on displaying the output to the user, you can instead read the entire file and then strlen .

 $data = file_get_contents($filename); $size = strlen($data); 
+10
source

What function are you using?

Since exec () can directly assign the result to a variable, so it may not be necessary to save the output to a file if you just want to load it into PHP.

0
source

You speak:

I use PHP to invoke the Java command, then move its result to a file called result.txt.

Who writes the result?

1. JAVA program?

2. You will catch the output in PHP and write it to a file.

3. Are you just redirecting output from the command line?

If 1 and 3 you may have a delay between when the result is written to the file, so practically when you read the file in PHP, if you do not wait for the completion of the execution, read it before it was even written with the result.

0
source

Source: https://habr.com/ru/post/897035/


All Articles