Jump to content
xisto Community
TavoxPeru

How To Force A Zip File To Be Downloaded

Recommended Posts

Hi, i have a problem with a zip file that i want to be force downloaded by any client, i know that with the header function it can be achieved but dont know why it doesnt works. I have two files, the first one is a simple html file with an a tag and the second is a php file, here is my code:
html file:

<body><a href="downpos.php">Download File</a></body>
Php file:
<?phpheader('Content-Type: application/zip');header('Content-Disposition: attachment; filename="file.zip"');readfile('downloads/file.zip'); ?>
May be i forgot something, does it is necessary to include a call to the exit function as the last code????

Best regards,

Share this post


Link to post
Share on other sites

try this:

<a href="download.zip">Download File</a>
as a link on the original page.
That was the first thing i have, but since that don't work i try to force the download with the header function as mention in my previous post.

I make another test using your suggestion and the link still dont work, so i make another test -yes another :P- but this time by adding the target property to the link and it works fine :P the file is downloaded. I dont know why it works but i guess that it is because the original html file is loaded in an iframe. So the solution is this:

<a href="download.zip" target="_blank">Download File</a>
Best regards,

Share this post


Link to post
Share on other sites

If you want to force the files to be downloaded here is an example how to do it, this is very handy if you want the .txt file to be downloaded and not to be shown in the browser or any other file which can be opened in a file, a very good practical issue to use this are PDF files. :P

/* Force browser to Download backup File and Set Headers */			ob_end_clean();			header('Content-Encoding: none');			header('Content-type: application/force-download');			header('Content-Disposition: attachment; filename="'.$s['file'].'.dat"');			header('Cache-Control: must-revalidate, post-check="0", pre-check="0"');			header('Content-Length: '.filesize($s['path']));			/* Output the File to User */			set_time_limit(0);			readfile($s['path']); 			exit;

the variables $s['file'] is a file name and $s['path'] is the full path to the file. :P

Share this post


Link to post
Share on other sites

Just a question for myself and others who read this thread. When you say "force to be downloaded," does that just mean that the browser won't open it, or on the "Do you want to save this file to disk or open with application" prompt, the only option is save to disk?~Viz

Share this post


Link to post
Share on other sites

Just a question for myself and others who read this thread. When you say "force to be downloaded," does that just mean that the browser won't open it, or on the "Do you want to save this file to disk or open with application" prompt, the only option is save to disk?

 

~Viz

 

It means that the prompt you mention will appear, as opposed to the browser showing the "content" (a bunch of useless signs) of the ZIP file. Forcing something to be downloaded means to let the browser know it's a file, and not a web page. As you know, most PHP files generate HTML code (or other types of markup), but this script generates a ZIP file, which necessitates the "header('Content-Type: application/zip');" line, but unfortunately, most browsers give us web-programmers problems with downloading files, which is why Quatrux's long hack is needed.

Share this post


Link to post
Share on other sites

It means that the prompt you mention will appear, as opposed to the browser showing the "content" (a bunch of useless signs) of the ZIP file. Forcing something to be downloaded means to let the browser know it's a file, and not a web page. As you know, most PHP files generate HTML code (or other types of markup), but this script generates a ZIP file, which necessitates the "header('Content-Type: application/zip');" line, but unfortunately, most browsers give us web-programmers problems with downloading files, which is why Quatrux's long hack is needed.

 

How about the use of mod_rewrite to solve the problem. The browser will think that the script is a zip file and the server will parse it as PHP!

 

RewriteEngine onRewriteRule ^file\.zip$ downpos.php?

You would still need some header information for it to work but since the browser sees a ZIP extension and that is what is sent, there shouldn't be much of a problem.

 

It seems that the smarter they make the browsers the dumber they act!

 

This also gives the "ZIP" sript the illusion of being an actual file instead of a script.

 

vujsa

Share this post


Link to post
Share on other sites
http://ww2.codeless.org/?folio=7POYGN0G2

This could help in your problem since the coding is somewhat similar to what you have maybe a bit different.

HTML
<a href="download.php?file=file.extension">download</a>

PHP
<?php// Type, leave this unchangedheader('Content-type: application/force-download');// What the file will be called if downloaded$file=$_GET["file"];if(file_exists($file)){header('Content-Disposition: attachment; filename="'.$file.'"');// The file source...readfile($file);}?> 

Share this post


Link to post
Share on other sites

This is the same, mine is a bit better, due to the right all the right headers is sent and the above script from that site won't work if output buffering is on and/or is using gzip encoding and it depends on the filesize of the file, so it is recommended to set time limit to zero, that means no time limit to not brake/corrupt the file in any way. I think I gather everything from php manual user comments and stuff and wrote that script to download banned tables, but it can used for anything..As he said, it might have been the problem because of frames that he needed to use target="_blank" but using it, it won't validate as xhtml anymore, so through htaccess or as I know through CPanel you can change how the extension (content type) is treated from the server. For example, some old servers don't know how to treat a RAR archive file, so it just parses it as text into the browser, which is default.

Share this post


Link to post
Share on other sites

Thanks everybody for take the time to reply my post, i confirm that the use of frames is the problem. I test the solutions made by saint-michael and Quatrux and it works perfect when i set the target property to _top or _parent, and when it is set to _blank it works too but the new window remains open, never works when it is not set or when it is set to _self or the name of the frame.

 

And with the code of saint-michael if the file is under a folder like foldername/filename.ext it works but the Save File dialog assumes that the name of the file to download is the name of the script without the extension.

 

BTW, i dont try the solution of vujsa because i'm not an expert with htaccess files. :P

 

Best regards,

Share this post


Link to post
Share on other sites

I've seen a couple sites which instead of opening up the "open or save" dialog box only open up a save dialog box. Does anyone know how to accomplish that. The only thing I can think of would be javascript, but I'm not sure exactly how that would work, and I don't remember seeing any javascript to do that on the pages I've visited.~Viz

Share this post


Link to post
Share on other sites

My searching is coming up blank on that, I think the only you can force a save as if you set up a javascript that uses the right click save as option. I did something on forcing mp3s to download instead of streaming into a music player.

Is that what your referring to viz?

https://wordpress.org/support/topic/force-download-of-mp3-files-instead-of-streaming/ https://g'>https://g https://g'>https://g'>https://wordpress.org/support/topic/force-download-of-mp3-files-instead-of-streaming/ https://g'>https://g https://g'>https://g

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.