filesize() return 0 ?
Asked Answered
S

1

8

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

this is the code what 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 the function "filesize()" is always returning 0,, but if i remove the code above it and put echo filesize($file_name); only in the page .. it return the right value (size of file), the script also make only 1 file even it became more than 1MB because the filesize is 0 as filesize() function return !.

i checked this question PHP Problem : filesize() return 0 with file containing few data? but it still not working even i follow the answer #1

i wish that you understand my problem.. and im sorry for my english.

Savagery answered 10/9, 2011 at 6:3 Comment(3)
try to close and reopen file,then write in it.I think it's because of buffer.Antiproton
@Moein7tl i tried, but didn't workSavagery
@Lawrence Cherone $file_name is in the header of the page, it contains a string value and its file.txt.Savagery
T
11

function filesize is cached internally
you need to explicitly use clearstatcache after each write

beside this, the usage of file_put_contents does not seem to be correct

please refer to the manual for more information

Triboelectricity answered 10/9, 2011 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.