Before posting the question, I explain the environment in which I need to develop the solution.
We have the file index.php, where the user has a textarea and a submit button in a form. In this textarea the user will paste a series of lines from an excel, each line contains the name of a file without an extension, something like
file1
file2
file3
...
fileN
Well, once the button is clicked, what I need to manage (I think AJAX would be ideal) is that a request is launched to the 'download.php' file (which already does what it has to do correctly).
The grace of this is that, let's say that fileN is file400 for example. We are not going to launch 400 requests at the same time by ajax towards the download.php file, it is absurd, so what I am trying to do is limit a maximum of simultaneous requests towards that file, for example 4, and while there are 4 download files .php "working" on its stuff, the rest of the files are queued, waiting for one of the busy ones to be released, and then to be able to make the new request.
In the following code, it requests all the files at the same time, but as I said, the idea is that there is a limit and the rest remain queued.
All the variables have their correct values and reach the script well, and what the php file actually does is go to an already defined ftp, it captures the file that we passed to it by POST, copies it to various defined locations as well, and then prints OK or ERROR {fileName} which is what we collect in the function(response) and print it on the screen to see the progress.
var cantidadFicheros = arrFiles.length;
for (var i = cantidadFicheros; i >= 0; i--) {
$.ajax({
type: 'POST',
url: 'download.php',
data: { fichero:arrFiles[i], ruta:ruta },
success: function(respuesta) {
$(resultado).append("<p>" + respuesta + "</p>");
},
error: function(respuesta) {
$(resultado).append("<p>" + respuesta + "</p>");
}
})
}
The code that I have put to do what I was commenting on, the first 4 files enter, they begin to be downloaded and when the first one finishes, the first in the queue enters and the rest continue waiting. The files can be of any size, from a few kb to several Gb.
Let's see if we can get it together with a little help :)
Try a list from which items are removed until there are no more.