Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:54:03+00:00 2026-05-26T06:54:03+00:00

I am making some consecutives(recrusive) ajax request to a php file that writes request

  • 0

I am making some consecutives(recrusive) ajax request to a php file that writes request parameter to a 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 returns to the ajax request, before it closes the text.txt file, so the next ajax requests that is send by the recrusive get an Access Denided error because seems that the file is still open by the previrios ajax request (event it has returned)? I tested this script even with async=false but I got the same error? How I can avoid that php scripts returns before it closes the file?

NOTE: I am not using session, I just send data to server for saving in a file.

NOTE2: Here I made a simple example, in realty I am using this method for uploading file by chunks with 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);

NOTE3: OK actually founded a workaround to avoid that error, in 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 i get access denided error in the first case, so waiting for comments!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:54:04+00:00Added an answer on May 26, 2026 at 6:54 am

    Does any content at all appear in the target file(test.txt)? I.e. does it fail after the first request is made or does it fail right away? The error suggests that the server cannot write to the file. This could be either because some other process is using the file or because the web server doesn’t have write permissions to the file. Just to eliminate the obvious, make sure that your web server is actually allowed to write to the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im making some of my ajax visible for google. To do that, I have
After making some changes in my models (eg. new field in a model and
While making some final tests of a class-library that I'm writing for Windows Mobile
I happened to be making some changes to a WordPress blog and noticed that
I am making some small business intelligence applications/tools that need to talk to other
I'm planning on making some dynamic PHP websites and I need a free Authentication
I am making some code , But it is so strange that code just
I'm in the process of making some improvements to a live Drupal site that's
I'm making some kind of movie with a webpage. For that I have to
I'm making some JSP tag. I pass a type parameter to the tag. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.