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

后端 未结 3 1212
闹比i
闹比i 2021-01-18 06:14

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() retur

3条回答
  •  天命终不由人
    2021-01-18 06:29

    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);
    

提交回复
热议问题