473,557 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help: Make a php file return a zip file?

I need help with writing a script that returns a zip file.

if(1 == $download){
// Setup headers for zip file
// Return zip file
// Afterwards, display previous php page
}

Can you help with fleshing out the function. I think I have the basic
idea of what needs to be done, but I don't know what the syntax is.

Thanks!

Jul 17 '05 #1
8 5115
ni*******@gmail .com wrote in
news:11******** *************@f 14g2000cwb.goog legroups.com:
I need help with writing a script that returns a zip file.

if(1 == $download){
// Setup headers for zip file
// Return zip file
// Afterwards, display previous php page
}

Can you help with fleshing out the function. I think I have the basic
idea of what needs to be done, but I don't know what the syntax is.

Thanks!


Not sure I understand the flow you're trying to achieve, but I'll try and
help a little. Let's assume you have a generic link on a web page and when
the user clicks the link you want to shoot them an arbitrary zip file (ie,
a zip file determined NOT by the link itself, but by the script the link
calls.)

<A HREF="shootzip. php">Click here for file</A>

Now, shootzip.php looks something like this:

<?

$zipfile = <name of zip file you want to send>;
$theirfile = <name of zip file you want the prompt box to default to on the
user side>;'

header("Content-Disposition: attachment; filename=\"$the irfile\"");
header("Content-type: application/octet-stream; name=$theirfile ");

readfile( $zipfile );

?>
This will *not* open a new window and the user will stay on the page w/ the
link. You could use Java script to get more fancy.

Is this kinda what you were looking for?
Jul 17 '05 #2
ni*******@gmail .com wrote:
I need help with writing a script that returns a zip file.

[ ... ]

Ehtor wrote:
header("Content-Disposition: attachment; filename=\"$the irfile\"");
header("Content-type: application/octet-stream; name=$theirfile ");


The registered MIME type for zip files is application/zip,
the IANA's definition of which does not allow for any
parameters.

http://www.iana.org/assignments/medi...pplication/zip

The often misused octet-stream subtype indicates that the
body of the message is 'arbitrary binary data', which is
inappropriate if a subtype is registered that better
describes the data.

http://www.ietf.org/rfc/rfc2046.txt

By the way, 'name' as a parameter to octet-stream was
deprecated over eleven years ago, in favour of the C-D
header.

--
Jock
Jul 17 '05 #3
John Dunlop <us*********@jo hn.dunlop.name> wrote in
news:MP******** *************** *@News.Individu al.NET:
ni*******@gmail .com wrote:
> I need help with writing a script that returns a zip file.


[ ... ]

Ehtor wrote:
header("Content-Disposition: attachment; filename=\"$the irfile\"");
header("Content-type: application/octet-stream; name=$theirfile ");


The registered MIME type for zip files is application/zip,
the IANA's definition of which does not allow for any
parameters.

http://www.iana.org/assignments/medi...pplication/zip

The often misused octet-stream subtype indicates that the
body of the message is 'arbitrary binary data', which is
inappropriate if a subtype is registered that better
describes the data.

http://www.ietf.org/rfc/rfc2046.txt

By the way, 'name' as a parameter to octet-stream was
deprecated over eleven years ago, in favour of the C-D
header.


Yes, you're correct. A shame you didn't actually answer the poster's
original question with your wealth of knowledge.

For the original poster's question, here's the revised PHP code:

header("Content-Disposition: attachment; filename=\"$the irfile\"");
header("Content-type: application/zip");
readfile( $zipfile );
By the way, if you review email headers, you'll note that many MIME
messages still include the name parameter in the C-T header. It should
also be noted that the original solution will work to serve ANY binary data
-- including ZIP.

Jul 17 '05 #4
Ehtor wrote:
A shame you didn't actually answer the poster's original
question
Sorry, but if you thought I was here foremost to answer
questions, then I'm afraid you were mistaken.
with your wealth of knowledge.
Hardly. The misuse of application/octet-stream is so
widespread that to recite chapter and verse on it is nothing
out of the ordinary.

[ ... ]
By the way, if you review email headers, you'll note that many MIME
messages still include the name parameter in the C-T header.
While that may be, the MIME spec says the name parameter is
deprecated; further, the previous spec, published in 1993,
also said it was deprecated. That's deprecated, not merely
discouraged. That other implementations haven't cottoned on
yet is no license for you to disregard the spec.
It should also be noted that the original solution will work to serve
ANY binary data -- including ZIP.


What reason is there to send zip files as application/octet-
stream when application/zip has been registered?

--
Jock
Jul 17 '05 #5
John Dunlop <us*********@jo hn.dunlop.name> wrote in
news:MP******** *************** *@News.Individu al.NET:
Ehtor wrote:
A shame you didn't actually answer the poster's original
question


Sorry, but if you thought I was here foremost to answer
questions, then I'm afraid you were mistaken.


Guess so. Why are you here?

with your wealth of knowledge.


Hardly. The misuse of application/octet-stream is so
widespread that to recite chapter and verse on it is nothing
out of the ordinary.

[ ... ]


Maybe there's a reason for that...

By the way, if you review email headers, you'll note that many MIME
messages still include the name parameter in the C-T header.


While that may be, the MIME spec says the name parameter is
deprecated; further, the previous spec, published in 1993,
also said it was deprecated. That's deprecated, not merely
discouraged. That other implementations haven't cottoned on
yet is no license for you to disregard the spec.


I have no idea what "cottoned on" means... But at least you used "yet"
to imply that 12 years is not long enough...

It should also be noted that the original solution will work to serve
ANY binary data -- including ZIP.


What reason is there to send zip files as application/octet-
stream when application/zip has been registered?


How about simplicity of code when delivering varying/multiple filetypes?
Not all standards (registered or not) suit individual business needs.
Respecting the RFC's is different from hiding behind them. Now, the
orignal poster has enough info to make an informed decision that is
appropriate for their circumstance.

I agreed that you were correct in your original reply post. I agreed
that, without knowing more about what the poster was trying to do, that
MIME-zip was the spec-correct solution. My objection was to your
lecturing instead of educating style. I'm over it now.
Jul 17 '05 #6
Ehtor wrote:
[John Dunlop wrote:]
Sorry, but if you thought I was here foremost to answer
questions, then I'm afraid you were mistaken.


Guess so. Why are you here?


First, to learn. Second, to give a little back. Neither
reason necessarily entails answering questions.

Nobull summed it up:

'Get real! *This is a discussion group not a helpdesk. You
post something we discuss its implications. If the
discussion happens to answer a question you've asked that's
incidental.'

news:u9******** ****@wcl-l.bham.ac.uk

[ ... ]
What reason is there to send zip files as application/octet-
stream when application/zip has been registered?


How about simplicity of code when delivering varying/multiple filetypes?


I don't expect anyone to serve files using complicated code.
The file type is known in advance here, so it doesn't get
much simpler. If the file type is unknown, though, PHP can
determine it, or at least try to:

http://www.php.net/manual/en/functio...ntent-type.php
Not all standards (registered or not) suit individual business needs.
OK, a situation could arise where breaking a rule is
justified. I mean, nobody expects rigid adherence to every
single specification. (For starters, specifications
themselves sometimes contain errors.)

[ ... ]
Now, the orignal poster has enough info to make an informed decision
that is appropriate for their circumstance.


Yes, good.

--
Jock
Jul 17 '05 #7
I was able to use Ethor's suggestion to fix a problem I'm having. M
original problem was that I was missing the quotes around th
filename in the CD header. I've read the ensuing dialog between Etho
and JD and have a question/comment driven by the fact that thing
don't seem to work for me the way they're "supposed to" and I wonde
why

I use a content type application/zip, and a content disposition wit
the file name (test.zip) just like JD suggests. But it only works i
the HTTP request ends with ".zip".
My request i
http://a.b.c.net/cc/production.php?file=123&zip=.zi
I get a valid zip file downloaded, but the filename isn't the filenam
in the CD header - its cc.zip

Furthermore, if I make the request
http://a.b.c.net/cc/production.php?file=123&zip
(no .zip on the end, which is what I wanted it to be), it doesn't wor
at all. The dialog box that pops up is making the filename
?file=123&zip

(Note: I'm using IE as the browser. My server is remote from where
am, and I go through all kinds of route point and a VPN to get to th
server. Also note that this works when I browse to my local machine.

I've tried various combinations of application/zip, octet-stream
lower/upper case, quotes/no quotes, order, etc. All have the sam
result described above. The only two headers I specify in PHP are th
CT and the CD

I making a SWAG that some device between my browser and the remot
server is monkeying around with the request and/or response headers
Otherwise why would the response headers that my PHP application ha
correctly applied? Is there something in the Apache or PH
configuration I need to look at
http://eye.cc -php- web design
Jul 17 '05 #8
dougerz wrote:
My original problem was that I was missing the quotes around the
filename in the CD header.
In spite of HTTP/1.1, the quotes are not usually necessary.
If the filename is short and doesn't contain certain special
characters, then you shouldn't quote it. Quotes are
necessary only if the filename is long or contains those
special characters. The details are laid out in RFC2183, if
you're interested.

http://www.ietf.org/rfc/rfc2183.txt
I've read the ensuing dialog between Ethor and JD and have a
question/comment driven by the fact that things don't seem to work
for me the way they're "supposed to" and I wonder why.
'Supposed to' isn't quite right. Since the C-D header isn't
part of HTTP, it's quite understandable why some browsers
don't observe it. I'm sure many simply ignore it. Besides,
C-D is nothing more than an indication; it doesn't force.

Me, well, I don't think C-D is useful to the web.
I use a content type application/zip, and a content disposition with
the file name (test.zip) just like JD suggests. But it only works if
the HTTP request ends with ".zip".
[ ... ]

I wouldn't expect perfection in every implementation, much
less Internet Explorer, by no means the paragon of web
browsers! The thing to do, as a provider, is publish
material in accordance with the interworking specifications.

There is a FAQ addressing this, but I don't know how helpful
you'll find it or if it'll reveal anything new.

http://www.htmlhelp.com/faq/cgifaq.3.html#22

Also, this could soon be an entry in the comp.lang.php FAQ
(nudge, nudge, anyone willing), so stay tuned.
I making a SWAG that some device between my browser and the remote
server is monkeying around with the request and/or response headers.


You can check what the headers are with

http://www.delorie.com/web/headers.html

Slàinte!

--
Jock
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2311
by: Mike Brashars | last post by:
Hi all, I have been searching for a week and am unable to find and example to "Populate picklist from directory and return file name". I have a php script that reads a log file and plots a graph. Right now, the log name is hard coded. The logs are archived each day in the form of say ddmmyy.log and I would like to put a drop down list in...
1
1055
by: bala | last post by:
Hi all sorry for the repost the earlier one was incomplete My problem is like this I am using the file control now i want that when the user browse for file only image files should be visible means I want to apply the filter One way I have tried was added a accept="image/jpg,image/gif" attribute < input type="file" id="file" type="file"...
2
2947
by: Bruce Russell | last post by:
This may sound stupid but I can't rename the WebForm1.aspx in the solution explorer. The file is located in my local web server at C:\Inetpub\wwwroot\Lab3-VB-Starter\WebForm1.aspx Is there some configuration problem in my IIS setup? Thanks,
19
2224
by: **Developer** | last post by:
When I get the image from the file the file remains locked so the Delete fails with a "used by another process" So I tried using a clone and disposing the obtained image. But that didn't fix the problem. Can you help?
0
1184
by: shakthi | last post by:
Hi, Am new to MFC programming.Please help me with the following problem.I want to select a file that is displayed in an edit control and when i click a button,the contents of that file should be displayed.I use Open() function to open a file..The file opens,but how to view its contents? I dont want the file contents to be displayed in a...
1
1961
by: rajarya | last post by:
Hi , I need to read a file(xml file) froma location in server by my client side HTML+Javascript code ,I did this using JSP,but now my requirement has been changed and I donot want any server side involve ment in this. Application will be like this 1.HTML/JSP page on the time of loading will be provided with a XML files name with proper...
2
1919
by: pandurusankar | last post by:
Hi, Need a pointer for managing File type button in HTML page, i am able to handle all the components in HTML using IEAutomation module like: text box, links, radio button, list box etc. but i need help in handling File type (Upload button). Thank you very much inadvance. reg JAI
0
1796
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner; public class project { public static void main( String args ) {
8
14927
by: raylopez99 | last post by:
I have the latest version of Visual Studio 2008 Professional, which allows you to create resource files (this is the .resx file, no?), unlike the Express version, which does not. I am trying to cut and paste code that MSDN recommends for playing a simple wav file from inside an embedded file, like presumeably the 'resources' file .resx is. ...
0
7830
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8061
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6180
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5454
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1161
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.