Reading and writing problems

Asked

Viewed 135 times

2

Assuming you have an access log, each request will create or include values in the log file.

Simulating several requests via ajax, I found that there is always a problem. If you log into 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 by ajax will be recorded a log file, but at a certain point a type error is triggered Permission denied or Invalid argument. I tried to see file_get_contents, file_put_contents, fopen, fwrite and always some error occurs.


The file manipulation works properly, I write and read without problems. I looped in 1000 and no errors occur during the recording.

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

  • Have you checked the directory permissions?

  • @gmsantos, Manipulate the files without problem, never gave error, only in ajax that hangs. It even records and overwrites, but at some point (I don’t know why) in the ajax loop returns Invalid argument

  • Only for testing, of a chmod -R 777 in your directory

  • @gmsantos It did not help.

  • 1

    I could not reproduce your problem. Here it worked without any problem.

Show 1 more comment

1 answer

1


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

Example: If the arquivo01.php is with the open file (fopen), php. or even another process of arquivo01.php cannot open it while the process that is open with it gives a fclose or be killed by the operating system.

Your loop presents no problems because you open the file closes 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
}
  • That’s right, thank you

  • It doesn’t make sense the answer would have solved your problem because according to the explanation, you wouldn’t have to change anything. I’m sure you didn’t forget to mention something or add anything else that might have solved the problem?

  • @Brunoaugusto, the answer was an explanation of the problem, and in this case, the manipulation of a file by multiple instances is an impediment of PHP. It doesn’t really solve the problem, but it tells the reason why it can’t do it. Since there is no solution, I thought the explanation could end the question.

  • But are there multiple instances? Because once you close the appeal, the instance no longer exists.

  • @Brunoaugusto, 2 or more simultaneous. There is no way to 'line up'. The loop by js simulates this.

  • And if you open the file outside of the loop by modifying the opening mode to to?

  • @Brunoaugusto, this js is a simulation that I did, it can be easily injected into any site by the browser console. I’ve used all read and write modes and php fails if it’s already open. If you want you can do a local test and send you the codes.

  • Guys sorry more the question was why was the error being denied permission not how to fix it, I know why happening now how to go around is already another story (question)

  • And it’s not Php that does it. That answer is from the operating system.

  • A process is open when Php tries to open S again.

  • As an example open a Word doc and try to delete it. These are OS treatments

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.