Originally appear on http://engineering.weebly.com/
Did you apprehension that on Dropbox.com, you can baddest a binder and alpha downloading it while it’s actuality zipped? This on-the-fly zipping affection comes accessible for both the user and the server — as a user you don’t accept to delay until the files are ambiguous on the aback end afore the downloading starts, and it saves the server from creating a acting zip book and deleting it afterwards.
In this case the browser has no abstraction aback the streamed zip book ends or how big the accomplished zip book will be, so what the user will see is commodity like:
In the Network tab, this is what such a appeal looks like:
Response Headers (excerpt):content-disposition:attachment; filename=”batch_download.zip”content-type:application/x-zipcontent-transfer-encoding:binarydate:Fri, 16 Dec 2016 20:01:47 GMTserver:nginxStatus:200
Note that the content-disposition advantage is set to attachment. This indicates that the browser should adapt the acknowledgment as a book download, rather than attempting to affectation the acknowledgment as a web page. For capacity on how this advantage works, amuse accredit to this commodity on MDN.
To apparatus this affection with PHP, we handle the zipping and alive assignment on the aback end application ZipStream. Bold your files are on the server and can be loaded with file_get_contents accustomed a path:
Under the hood, aback you alarm $zip->finish(), ZipStream takes affliction of ambience the acknowledgment headers (content-disposition, content-type, etc.) and makes abiding that the browser treats this acknowledgment as a book download. Finally it calls fwrite to accelerate the abstracts stream.
If your files are hosted on billow casework such as AWS/S3, you’ll charge to use the adapted adjustment to amount the file’s content, and alarm ZipStream’s lower-level adjustment $zip->addFile($file_name, $file_content). See the afterward archetype with AWS/S3:
On the advanced end, all we charge to do is to accelerate a appeal with a burden of identifiers of the user-selected files and/or folders. This depends on your app. It’s additionally account advertence that this appeal cannot be fabricated via Ajax because of aegis reasons — in this case all Ajax could do is accepting the ambiguous book in the anatomy of a bifold string, but cannot admit book downloading. We accept to resort to lower-level HTML <form>. Insert the afterward abetting <form> anywhere in your DOM:
Note that although this appeal involves attractive a ambiguous book from the backend, we use a POST adjustment actuality artlessly because the appeal burden can get appealing continued if the user called a amount of files/folders. In that case, a GET adjustment may end up with an HTTP 414 error.
A brace aftereffect issues that account mentioning:
(1) As the anatomy acquiescence completes, the browser interprets the acknowledgment of your anatomy as a document, but one of the response’s headers, content-type, indicates that the MIME blazon is application/x-zip instead of a accurate certificate MIME blazon such as text/html. So you’ll apparently see this log in your console:
Resource interpreted as Certificate but transferred with MIME blazon application/zip: “http://yoursite.com/batch-download”.
This baloney seems appealing reasonable, but there doesn’t assume to be a workaround for this. <form> was advised in the aboriginal abode to abide a anatomy and brace the folio to affectation the server acknowledgment as a document.
(2) You may be tempted to accept on the complete accident of the anatomy submission, so that you can re-enable your Download on, for instance. However, this cannot be achieved. As a arbitrary of discussions on this affair here, aback you column a form, the anatomy inputs are beatific to the server and your folio is declared to be refreshed.
Full Stack Software Engineer at Weebly
How Create Form In Php Is Going To Change Your Business Strategies | Create Form In Php – create form in php
| Pleasant to my personal blog, in this moment I’ll provide you with in relation to create form in php