Link to home
Start Free TrialLog in
Avatar of SiemensSEN
SiemensSEN

asked on

Zip and download file php ajax

Hello,
  I have a an application that display  some file data to the user E.g

[]|filename| size

When the user select one or more files. I would like to post the selected file  information back to the server. Then server then zip the file(s) and send the data to the browser.

How can I do this with an Ajax call?. I have server side script that accept the file data and contruct the zip file. How do I send the zip back to the browser and have it force the user to save or open the file.

Thanks
SOLUTION
Avatar of Erdinç Güngör Çorbacı
Erdinç Güngör Çorbacı
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Ray , Very extensible approach i'll add this to my helpers library with some changes , thanks.

But i have a question ; Do this usage loads file completely to our server?
If so, does, passing file completely through php, slows down server or result with a timeout ?
- if the file is big or/and from a slow remote host-

For instance for a mkv file about 500MB, which is on a remote server.
Yes, this will pass the entire contents of the file through the server.  Its purpose is to cause a download, instead of the normal action associated with the browser.  If the normal browser action is acceptable (render HTML or images, open a PDF, etc), there would be no need to force a download.
Avatar of SiemensSEN
SiemensSEN

ASKER

Thanks,
I will use the download script but call it from my Ajax script

This is what I am trying to do---

I have  a grid where users can select multiple files. When the "Download" button is press , I get all the selected files. Then send it to the server via ajax call . On the server I have a method that get the files and compress them into a zip file. Finally, I would like to render the file to browser The browser should  force the user to save or download the zip file

Thanks erdincgc.

 no issue ... did not know how to do it
In case you are planning to use zip functions via PHP, you might want to reconsider.  It can be very difficult to get the zip archive right.  If it were my task, I would go in the direction of zipping the files and storing the zipped version, rather than using PHP to create a zip archive on the fly.  Just a thought.... ~Ray
The problem is the file are already zip . Based on some user selected criteria , the user will be presented with multiple zip files. They can then select one or more. zip files  Then, the application should download the file .

I was planning to get all the zip file (size limit) and combining into one zip archive and then send it to the browser for download

for example , my display is in a grid below. So, the user can select all .

e.g display
<download>
[] file| size
[] f1.gz |1mb
[] f2.gz | 2mb
[] f3.gz |1mb

Ray what would be the best approach
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't know the end structure of your site but if you can building grouped zip files and let visitor to choose group rather than each one by one may be a better approach if your options are not so many building every combination first maybe much more better to reduce server loads (while building zip files).

Or let user download each file one by one , most of sites use this method.

But if you are confident about your server you may go straight on. Even In this option you may create combinations in specific formatted filenames such as ;

if file1 file2 and file 3 is checked
make combined file name as file1-file2-file3.zip.
And then if you check file existence before building again.

You may gain some cpu load from the ones built before.
Thanks .
I used the code provided to  create a php file "download.php".Then call it via ajax..

Here my code

<iframe id="frameDownload"></iframe>
<script>
$.ajax({
             url:"myurl",
             type:"POST",
             datatype:"text",
             data:data,
             success:function(res)
             {    
                 var url = "/download.php?f="+res;
                 $('frameDownload').src=url;
                  window.location = url
             },</script>