473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create 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 5101
ni*******@gmail.com wrote in
news:11*********************@f14g2000cwb.googlegro ups.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=\"$theirfile\"");
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=\"$theirfile\"");
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*********@john.dunlop.name> wrote in
news:MP************************@News.Individual.NE T:
ni*******@gmail.com wrote:
> I need help with writing a script that returns a zip file.


[ ... ]

Ehtor wrote:
header("Content-Disposition: attachment; filename=\"$theirfile\"");
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=\"$theirfile\"");
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*********@john.dunlop.name> wrote in
news:MP************************@News.Individual.NE T:
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
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...
1
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...
2
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...
19
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...
0
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...
1
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...
2
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...
0
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;...
8
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.