Question

I'm using PHP to download a (big) file from a remote server and this download is triggered by clicking on a download button on a web page.

So when i click the download button on the web page, then an Ajax request (with angulars $http) is made to a PHP function. That function triggers a download using cURL.

In the mean time I'd like to make other requests to my PHP site with Ajax. But all other Ajax requests show the status Pending as long as the download is in progress.

So basically the download is blocking every other request to PHP. Is there any way I can avoid this blockage?

Was it helpful?

Solution

This is most likely due to the session file being locked. This is a very common oversight on many php-based web-apps. Essentially, when you call session_start() to access the $_SESSION array, it opens the session file in the tmp directory in read/write mode and locks this file to avoid potential concurrency issues. If you call another script from a different ajax request (or any HTTP request, such as from a new browser window), if that second request also calls session_start, it will wait until the session file is unlocked before moving forward.

The fix is to release the session file once you know you are no longer going to be writing to it. Since your use-case is a huge file download, it is unlikely that during the data output you will need to push anything into the $_SESSION array. You release it from write-mode by calling session_write_close()

I had no idea this was the case until I found out a popular web-app I frequently use was guilty of this. A great blog post on this common bottleneck is:

http://konrness.com/php5/how-to-prevent-blocking-php-requests/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top