Filesize () return 0?

im trying to insert the array values ​​into a file using foreach .. and if the file size became up to 1 MB (1,000,000 bytes). script create a new file and put the value inside ...

this is the code i used:

foreach($array as $code){ if(filesize($file_name) < '1000000'){ $update_file = file_get_contents($file_name); file_put_contents($file_name,$update_file.$code); }else{ $open = fopen($file_name.rand('99999'),'w'); file_put_contents($file_name,$code); } echo filesize($file_name); } 

now the problem is that the "filesize ()" function always returns 0, but if I delete the code above it and put echo filesize($file_name); only on the page .. it will return the correct value (file size), the script also makes only 1 file, even if it has become larger than 1 MB, because the file size is 0, because the filesize () function returns !.

I checked this question PHP problem: filesize () returns 0 with a file containing some data? but it still does not work, even I follow the answer # 1

I want you to understand my problem .. and they are sorry for my English.

+6
source share
1 answer

The filesize function is cached internally.

you need to use clearstatcache after each write

Also, using file_put_contents doesn't seem to be correct

refer to the manual for more information

+8
source

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


All Articles