添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
酷酷的牛腩  ·  spring boot ...·  1 年前    · 
魁梧的小刀  ·  android 菜单键退出 ...·  1 年前    · 
无邪的黑框眼镜  ·  torch.cuda.empty_cache ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When I run this function on multiple scripts one script generated warning: fread(): Length parameter must be greater than 0

function test($n){
  echo "<h4>$n at ".time()."</h4>";
  for ($i = 0; $i<50; $i++ ){    
  $fp = fopen("$n.txt", "r");
  $s = fread($fp, filesize("$n.txt") );
  fclose($fp);
  $fp = fopen("$n.txt", "w");
  $s = $_SERVER['HTTP_USER_AGENT'].' '.time();
  if (flock($fp, LOCK_EX)) {  // acquire an exclusive lock
      fwrite($fp, $s);
      // fflush($fp);// flush output before releasing the lock
      flock($fp, LOCK_UN);    // release the lock
    } else {
      echo "Couldn't get the lock!";

I try to write reading of the file for multiple users, but only one user can write the file. I know that when I use fwrite with flock - LOC_EX, next scripts must wait till the write is finished. But here it seems like filesize doesn't wait till the write operation is finished. My opinion is that it tries to reach the file when the file size is 0, and as a result this produces the problem: 0 bytes will be read from the file, when it is written by original script.

Is it possible to fix this for fread function?

Purpose of this script is to test fread with some limit and to check the data which I read later, if the data are really written when I did not used fflush.

$fp = fopen("$n.txt", "w"); $s = $_SERVER['HTTP_USER_AGENT'].' '.time(); if (flock($fp, LOCK_EX)) { // acquire an exclusive lock fwrite($fp, $s); // fflush($fp);// flush output before releasing the lock flock($fp, LOCK_UN); // release the lock } else { echo "Couldn't get the lock!"; echo "Filesize must be greater than 0"; please change $s variables name its use same things two time Thank you. I just wanted to see if the file is read and it is. Then I change the content to the HTTP_USER_AGENT just for test – Johny Bony Oct 9, 2019 at 13:16 Wait, is this really solution? I need the script to wait till the flock of original script is released. If the fread is skipped so the file is not read at all, and this is not what I want to achieve. – Johny Bony Oct 9, 2019 at 13:18

The error occurs in the middle line of the above three lines.

Firstly, these three lines could be rewritten into a single line as follows:

$s = file_get_contents("$n.txt");

However, these isn't necessary, as these three lines are entirely redundant in your code. They don't do anything useful.

What they do is open a file, store its contents to $s and then close it.

But you are then immediately setting $s to a different value, thus throwing away the previous value, and making it pointless to have read it from the file in the first place.

If you need to keep the original contents of the file, then use file_get_contents() and make sure you don't overwrite the contents of the variable.

If you don't need the original contents of the file, then just delete those three lines from your code.

Incidentally, this error highlights a couple of good coding practices that you should take on board: Firstly, never re-use a variable for two different things, and secondly always give your variables (and functions) good names. $s is not a good name; $previousFileContents would be a better name; it would have made the error much more obvious.

I know that file_get_contents can do the job, but I am testing the fread. From my code it seems that it is not possible to use fread for sharing file operations. I wanted to test if is it possible to safely read some amout of bytes with fread (on shared operations). It seems that fread and file_get_contents works completely different. I cannot do fflush after file_get_contents, because file_get_contents does not accept filehandle. – Johny Bony Oct 9, 2019 at 13:51

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.