By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,456 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,456 IT Pros & Developers. It's quick & easy.

Download code

P: n/a
I have a set of jpg files on a page. I want to give the user the option to
downloadd all of them at once. What is the code to do that? It must exist
because many times users are presented with a download button and there must
be something behind it. Even if it is one file at a time, I can loop
through the list. As for a name, I can use the current name and if it is
one at a time, then the user can name each one.

Alternatively, I could zip it up into one file and then have the download.

So, I have two questions:

1 - What is the code to download a file to the user's computer?
2 - How do I zip up a set of jpg files on the server?

Shelly
Sep 8 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Sheldon Glickler wrote:
I have a set of jpg files on a page. I want to give the user the option to
downloadd all of them at once. What is the code to do that? It must exist
because many times users are presented with a download button and there must
be something behind it. Even if it is one file at a time, I can loop
through the list. As for a name, I can use the current name and if it is
one at a time, then the user can name each one.

Alternatively, I could zip it up into one file and then have the download.

So, I have two questions:

1 - What is the code to download a file to the user's computer?
This works for me. For a SINGLE file. In essence I call this from a URL
pointing to it with the file name as GET variable. This is via a
stament iek

<A HREF="filesend.php?filename="myfile.jpg">Myfile.jp g</a>

You will have to fudge your own ways to arrive at eh name, size and
filetype: the $content is the contents of the file: In my case from a
database. In yours you can just output the file:-
Filesend.php...fragment..
=========================
$name=$_GET['filename'];

// insert code to see if file exists, determine its size,

$mtype=get_mime($name); //get_mime() in my case parses /etc/mimetypes
and attempts a match from that to extension.

//spit out standard header stuff
header("Pragma: public");

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: public");

header("Content-Description: File Transfer");

header("Content-Type: ".$mtype);

header("Content-Disposition: attachment; filename=\"".$name."\"");

header("Content-Transfer-Encoding: binary");
print $content;
function get_mime($filename)
{
$default="application/force-download";
// first extract the extension
$array=explode(".",$filename); // split the name into the bits
separated by periods
$count=count($array);
if ($count<2) // if there IS NO extension..
return $default; // and let the user sort it out.
$ext=$array[$count-1]; // it will be the last element in the array..
$fp=fopen("/etc/mime.types", "r");
if(!$fp) return ($default); // no /etc/mime.types file
while (!feof($fp))
{
$buffer = fgets($fp, 128);
if (ctype_space($buffer{0}) || $buffer{0}=='#' || $buffer{0}=='\n')
continue; // skip empty lines. or lines starting with spaces
or hashes
sscanf($buffer, "%s %s %s %s %s %s \n",$mime_type,$extension,
$extension1, $extension2, $extension3, $extension4);
if ($ext==$extension || $ext==$extension1 || $ext==$extension2 ||
$ext==$extension3 || $ext==$extension4 )
{
fclose ($fp);
return($mime_type);
}
}
fclose($fp);
return $default;
}
2 - How do I zip up a set of jpg files on the server?
Oh..if you want to send a stream of all the checked fils in a ZIP?

I am sure there is a standard option in PHP for that.

>
Shelly

Sep 8 '07 #2

P: n/a

Sheldon Glickler wrote:
I have a set of jpg files on a page. I want to give the user the option to
downloadd all of them at once. What is the code to do that? It must exist
because many times users are presented with a download button and there must
be something behind it. Even if it is one file at a time, I can loop
through the list. As for a name, I can use the current name and if it is
one at a time, then the user can name each one.

Alternatively, I could zip it up into one file and then have the download.

So, I have two questions:

1 - What is the code to download a file to the user's computer?
2 - How do I zip up a set of jpg files on the server?

Shelly

as for zipping there is a decent class for that, easy to use, but
memory limits and so on might stop large jpegs being zipped, you can
also use 7zip command line version to reate an archive to disk which
you then serve as required. This is perhaps the best way depending on
whether you have shell access through php, as it can create many types
of archive, you then serve this file as normal

Sep 8 '07 #3

P: n/a
Sheldon Glickler wrote:
I have a set of jpg files on a page. I want to give the user the option to
downloadd all of them at once. What is the code to do that? It must exist
because many times users are presented with a download button and there must
be something behind it. Even if it is one file at a time, I can loop
through the list. As for a name, I can use the current name and if it is
one at a time, then the user can name each one.

Alternatively, I could zip it up into one file and then have the download.

So, I have two questions:

1 - What is the code to download a file to the user's computer?
2 - How do I zip up a set of jpg files on the server?

Shelly

Of course you do know they can always download an image by
right-clicking on the image and selecting "Save Image..." (or whatever
wording their browser uses) option. No PHP required.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 8 '07 #4

P: n/a

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:W-******************************@comcast.com...
Sheldon Glickler wrote:
>I have a set of jpg files on a page. I want to give the user the option
to downloadd all of them at once. What is the code to do that? It must
exist because many times users are presented with a download button and
there must be something behind it. Even if it is one file at a time, I
can loop through the list. As for a name, I can use the current name and
if it is one at a time, then the user can name each one.

Alternatively, I could zip it up into one file and then have the
download.

So, I have two questions:

1 - What is the code to download a file to the user's computer?
2 - How do I zip up a set of jpg files on the server?

Shelly

Of course you do know they can always download an image by right-clicking
on the image and selecting "Save Image..." (or whatever wording their
browser uses) option. No PHP required.
Of course I know that! However, most users are not overly computer literate
and I would have to give them explicit instructions on what to do. A button
saying "Download" is much easier on the SU. (stupid user). Also, I would
like to have a button that says "Download All Pictures". For either of
these I need code behind the button submit.

Shelly
Sep 8 '07 #5

P: n/a
Shelly wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:W-******************************@comcast.com...
>Sheldon Glickler wrote:
>>I have a set of jpg files on a page. I want to give the user the option
to downloadd all of them at once. What is the code to do that? It must
exist because many times users are presented with a download button and
there must be something behind it. Even if it is one file at a time, I
can loop through the list. As for a name, I can use the current name and
if it is one at a time, then the user can name each one.

Alternatively, I could zip it up into one file and then have the
download.

So, I have two questions:

1 - What is the code to download a file to the user's computer?
2 - How do I zip up a set of jpg files on the server?

Shelly
Of course you do know they can always download an image by right-clicking
on the image and selecting "Save Image..." (or whatever wording their
browser uses) option. No PHP required.

Of course I know that! However, most users are not overly computer literate
and I would have to give them explicit instructions on what to do. A button
saying "Download" is much easier on the SU. (stupid user). Also, I would
like to have a button that says "Download All Pictures". For either of
these I need code behind the button submit.

Shelly

I just put a note on the page as to what to do. Even the
least-knowledgeable users can figure that out.

But you'll have to zip the image(s) to ensure they are downloaded.
Otherwise the client browser may just display the image. You can't
control that from the server end.

For building zip files, you can start with:

http://www.php.net/manual/en/ref.zip.php
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 8 '07 #6

P: n/a
Of course you do know they can always download an image by
right-clicking on the image and selecting "Save Image..." (or whatever
wording their browser uses) option. No PHP required.
One issue here is that you might want to show a 'preview' image that's
small enough to load quickly and/or fit on the screen. But you want to
allow the visitor to download the full-resolution large image that is
not necessarily shown on the screen. So right-click would only work
for the browser-compatible images.

Sep 10 '07 #7

P: n/a
M. Katz wrote:
>Of course you do know they can always download an image by
right-clicking on the image and selecting "Save Image..." (or whatever
wording their browser uses) option. No PHP required.

One issue here is that you might want to show a 'preview' image that's
small enough to load quickly and/or fit on the screen. But you want to
allow the visitor to download the full-resolution large image that is
not necessarily shown on the screen. So right-click would only work
for the browser-compatible images.
That's possible, but that wasn't in the requirements the op posted.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.