Problems with reading and writing

2
Assuming you have an access log, each request will create or add values to the log file.

Simulating multiple requests via ajax, I've found that there is always a problem. If you enter the browser console and run the code below, a request will be made within the loop.

setInterval(function()
{
    $('div').each(function()
    {
        $.get( 'url interna' , function( data )
        {
            $('html').text( 'data' )
        });
    })
} , 100 )

Each request for ajax will be written to a log file, but at a certain point a Permission denied or Invalid argument error is triggered. I've tried via file_get_contents , file_put_contents , fopen , fwrite and always some error occurs.

The manipulation of the files works correctly, I write and read without problems. I looped 1000 and no error occurs during recording.

for( $i = 1; $i <= 1000; $i++ )
{
    $fp = fopen( 'file.txt' , 'w' );
    fwrite( $fp , 'texto' );
    fclose( $fp );
}
asked by anonymous 08.12.2014 / 02:51

1 answer

1

When the file is already opened by one process you can not edit it in another.

Example: If file1.php has the file open (fopen), file02.php or even another file01.php will not be able to open it while the process that is running with it fleshes out or is killed by the operating system.

Your loop has no problems because you open the file, close it, and so on ...

for( $i = 1; $i <= 1000; $i++ )
{
    $fp = fopen( 'file.txt' , 'w' ); //Abre o arquivo
    fwrite( $fp , 'texto' );
    fclose( $fp ); //Fecha o arquivo
}
  

To simulate a permission denied error you can use the code below ...

$abre_arquivo =  fopen( 'file.txt' , 'w' ); //Ele foi aberto ou seja o espaço de memória esta reservado para uso da variável $abre_arquivo.
for( $i = 1; $i <= 1000; $i++ )
{
    $fp = fopen( 'file.txt' , 'w' ); //Abre o arquivo - Porém ele já esta aberto em outro processo.
    fwrite( $fp , 'texto' );
    fclose( $fp ); //Fecha o arquivo
}
    
11.12.2014 / 19:28