Home › Forums › Back End › php download multiple files problem. › Reply To: php download multiple files problem.
Doesn’t that mean I can use the HTTP HEAD request to trigger the download since now the php download script won’t be sending the file contents back to the javascript?
There are two parts to an HTTP response: the “head” and the “body”. (Don’t confuse these with the <head>
and <body>
elements in an HTML document.) The head is all the metadata (Response Code, Content-Type, Length, etc.). The body is the response itself. When you use the HEAD method, you’re specifically telling the server that you don’t want the actual response: just the info about the response.
Is iframe the way EVERYBODY does downloads?
It’s very common, yes.
Downloading a file is no different than following a link to a webpage. In both cases, you’re making an HTTP connection and asking another computer for some data. It’s just a matter of what you do with the response. When you follow a link to a webpage, the browser knows to display the page. When you follow a link to a zip file, the browser knows to save it somewhere on your hard drive.
Ajax makes the same kind of HTTP requests, but it can’t save the response to your hard drive.
I’m coming to the conclusion that this is a limitation of the browser (not php/javascript) and maybe that’s why an iframe/new window is required for the download? Pretty stupid.
It’s not a technical limitation; it’s a security precaution. Javascript comes from another computer, and the browser knows better than to automatically trust it. Same with file downloads: the browser doesn’t ask if you want to download something just so you have the opportunity to choose where to save it, it asks to make sure you know what’s going on and that you really did mean to download the file.
So, JS can’t download anything on its own, but it can pass that action on to the browser, which will deal with it in a safe way. I don’t know how old you are, but if you were on the internet 10-15 years ago, you’ll recall that it simply wasn’t safe to visit a website that you didn’t already know and trust. Browsers not trusting javascript by default is definitely A Very Good Thing.
So is it possible to have javascript trigger another way like a hidden form/submit to execute my php scripts?
With javascript, a form isn’t strictly necessary, which is why I left it out of my example. The only advantage would be that you could make the download work even if javascript was disabled (though depending on the browser it would redirect to the new url).