File_put_contents access denied for multiple AJAX requests (but synchronized)?

I make a few (recrusive) ajax requests for a serial user to a php file that writes the request parameter to the file:

make_ajax(s) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange=function() { if(this.readyState == 4 && this.status == 200) { if(s>0) make_ajax(s-1) } }; xhr.open('POST','write.php?s='+s+'&string=somelongstring',true);//url + async/sync ... xhr.send(null); } make_ajax(15);//start request 

write.php:

 file_put_contents('test.txt',$_GET['s']); 

It seems that the server is returning to the ajax request before it closes the text.txt file, so the following ajax requests that are sent by recrusive get a Denend Access error, because it seems that the file is still opened by the previrios application an ajax request (event, which it returned)? I tested this script even with async = false, but got the same error? How can I avoid php scripts before it closes the file?

NOTE: I do not use a session, I just send data to the server to save to a file.

NOTE 2 . Here I made a simple example: in reality, I use this method to load a file in chunks using ajax and the mozSlice method. Full code:

  function uploadFileXhr(o,start_byte) { var total=o.size; var chunk; var peice=1024 * 1024;//bytes to upload at once var end_byte=start_byte+peice; var peice_count=end_byte/peice; $('#debug').html(peice_count); var is_last=(total-end_byte<=0)?1:0; chunk=o.mozSlice(start_byte, end_byte); var xhr = new XMLHttpRequest();//prepare xhr for upload xhr.onreadystatechange=function() { if(this.readyState == 4 && this.status == 200) { if(is_last==0) { uploadFileXhr(o,end_byte); } } }; xhr.open('POST','upload.php',true);//url + async/sync xhr.setRequestHeader("Cache-Control", "no-cache"); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');//header xhr.setRequestHeader('Content-Type', 'multipart/form-data');//type for upload xhr.send(chunk);//send request of file } uploadFileXhr(file_input,0);//start recrusive call 

upload.php:

 $flag =($_GET['start']==0) ? 0:FILE_APPEND; file_put_contents($remotePath.$add.$file_name, file_get_contents('php://input'),$flag); 

NOTE 3 .: OK really based a workaround to avoid this error, in the upload.php script:

  $flag =($_GET['start']==0) ? 0:FILE_APPEND; $file_part=file_get_contents('php://input'); while(@file_put_contents($remotePath.$add.$file_name, $file_part,$flag)===FALSE) { usleep(50); } 

But still I can’t explain why in the first case I get access to the error, so we are waiting for comments!

+4
source share
1 answer

Is all the content displayed in the target file (test.txt)? That is, is it not running after the first request or is it immediately crashing? The error indicates that the server cannot write the file. This may be due to the fact that some other process uses the file, or because the web server does not have write permissions to the file. To fix the obvious, make sure your web server is indeed allowed to write to the file.

0
source

All Articles