Download code

Download code

am 08.09.2007 05:35:29 von sheldonlg

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

Re: Download code

am 08.09.2007 12:51:22 von Courtney

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



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
>
>

Re: Download code

am 08.09.2007 13:09:07 von shimmyshack

On Sep 8, 4:35 am, "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

its easy but you have to make sure you dont accidentally allow people
to download _any_ file on your server.

//you must check the file requested is allowed to be served, or you
must put files you wish to be downloaded in a certain directory. It is
better to make this directory a private one - outisde the web doc
root, so it cant be directly accessed using a URL.



//$file is the complete path+name
$file = '/var/share/apache2/sitename/private/files/';
//using basename stops people who might use ..
//or whatever - it gets the filename from the path+name


//$filename is the file name
$filename = basename( $file );
//this is a quick way to get the extension, there are better ways
$strFileExtension = substr( $filename, (strrpos($filename,'.')+1),
strlen($filename));
//call a function which determines mimetype based on extension - there
are better ways!
$strMimeType = getStrMimeType( $strFileExtension );

$arrAllowedFileExtensions = array(
'jpg', 'txt', 'ppt' //etc for all extensions you want to allow
);

if( !in_array($strFileExtension,$arrAllowedFileExtensions) )
{
//log?
exit;
}

$filesize = filesize( $file );
header( 'Content-Type: ' . $strMimeType );
header( 'Content-Description: File Transfer' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Accept-Ranges: bytes' );
header( 'Content-Length: ' . $filesize );
header( 'Content-Disposition: attachment; filename=' . $filename );
//if using php4 check it doesnt have the > 2MB
readfile( $file );
exit;



here is the function which serves some of the more common files with
the mimetypes, people might disagree on what these mimetypes should
be, but heres what I used in a recent app (the new OpenOffice stuff is
here too)


function getStrMimeType( $strExtension )
{
switch( $strExtension )
{
case 'msi' :
$strMimeType = 'application/x-msi'; break;
case 'jpg' :
$strMimeType = 'image/jpeg'; break;
case 'jpeg' :
$strMimeType = 'image/jpeg'; break;
case 'jpe' :
$strMimeType = 'image/jpeg'; break;
case 'gif' :
$strMimeType = 'image/gif'; break;
case 'png' :
$strMimeType = 'image/png'; break;
case 'bmp' :
$strMimeType = 'image/bmp'; break;
case 'tif' :
$strMimeType = 'image/tiff'; break;
case 'tiff' :
$strMimeType = 'image/tiff'; break;
case 'ico' :
$strMimeType = 'image/x-icon'; break;
case 'asf' :
$strMimeType = 'video/asf'; break;
case 'asx' :
$strMimeType = 'video/asf'; break;
case 'wax' :
$strMimeType = 'video/asf'; break;
case 'wmv' :
$strMimeType = 'video/asf'; break;
case 'wmx' :
$strMimeType = 'video/asf'; break;
case 'avi' :
$strMimeType = 'video/avi'; break;
case 'mov' :
$strMimeType = 'video/quicktime'; break;
case 'qt' :
$strMimeType = 'video/quicktime'; break;
case 'mpeg' :
$strMimeType = 'video/mpeg'; break;
case 'mpg' :
$strMimeType = 'video/mpeg'; break;
case 'mpe' :
$strMimeType = 'video/mpeg'; break;
case 'txt' :
$strMimeType = 'text/plain'; break;
case 'c' :
$strMimeType = 'text/plain'; break;
case 'cc' :
$strMimeType = 'text/plain'; break;
case 'h' :
$strMimeType = 'text/plain'; break;
case 'rtx' :
$strMimeType = 'text/richtext'; break;
case 'css' :
$strMimeType = 'text/css'; break;
case 'htm' :
$strMimeType = 'text/html'; break;
case 'html' :
$strMimeType = 'text/html'; break;
case 'mp3' :
$strMimeType = 'audio/mpeg'; break;
case 'mp4' :
$strMimeType = 'audio/mpeg'; break;
case 'ra' :
$strMimeType = 'audio/x-realaudio'; break;
case 'ram' :
$strMimeType = 'audio/x-realaudio'; break;
case 'wav' :
$strMimeType = 'audio/wav'; break;
case 'ogg' :
$strMimeType = 'audio/ogg'; break;
case 'mid' :
$strMimeType = 'audio/midi'; break;
case 'midi' :
$strMimeType = 'audio/midi'; break;
case 'wma' :
$strMimeType = 'audio/wma'; break;
case 'rtf' :
$strMimeType = 'application/rtf'; break;
case 'js' :
$strMimeType = 'application/javascript'; break;
case 'pdf' :
$strMimeType = 'application/pdf'; break;
case 'doc' :
$strMimeType = 'application/msword'; break;
case 'pot' :
$strMimeType = 'application/vnd.ms-powerpoint'; break;
case 'pps' :
$strMimeType = 'application/vnd.ms-powerpoint'; break;
case 'ppt' :
$strMimeType = 'application/vnd.ms-powerpoint'; break;
case 'wri' :
$strMimeType = 'application/vnd.ms-write'; break;
case 'xla' :
$strMimeType = 'application/vnd.ms-excel'; break;
case 'xls' :
$strMimeType = 'application/vnd.ms-excel'; break;
case 'xlt' :
$strMimeType = 'application/vnd.ms-excel'; break;
case 'xlw' :
$strMimeType = 'application/vnd.ms-excel'; break;
case 'mdb' :
$strMimeType = 'application/vnd.ms-access'; break;
case 'mpp' :
$strMimeType = 'application/vnd.ms-project'; break;
case 'odp' :
$strMimeType = 'application/vnd.oasis.opendocument.presentation';
break;
case 'odt' :
$strMimeType = 'application/vnd.oasis.opendocument.text'; break;
case 'sxi' :
$strMimeType = 'application/vnd.sun.xml.impress'; break;
case 'sxw' :
$strMimeType = 'application/vnd.sun.xml.writer'; break;
case 'swf' :
$strMimeType = 'application/x-shockwave-flash'; break;
case 'class' :
$strMimeType = 'application/java'; break;
case 'tar' :
$strMimeType = 'application/x-tar'; break;
case 'zip' :
$strMimeType = 'application/zip'; break;
case 'gz' :
$strMimeType = 'application/x-gzip'; break;
case 'gzip' :
$strMimeType = 'application/x-gzip'; break;
case 'exe' :
$strMimeType = 'application/x-msdownload'; break;
default :
$strMimeType = 'application/unknown'; /*was octet*/ break;
}
return $strMimeType;
}

Re: Download code

am 08.09.2007 13:14:07 von shimmyshack

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

Re: Download code

am 08.09.2007 15:22:21 von Jerry Stuckle

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.
jstucklex@attglobal.net
==================

Re: Download code

am 08.09.2007 17:30:53 von Shelly

"Jerry Stuckle" wrote in message
news:W-ednUfe0eT3P3_bnZ2dnUVZ_tajnZ2d@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

Re: Download code

am 08.09.2007 21:07:50 von Jerry Stuckle

Shelly wrote:
> "Jerry Stuckle" wrote in message
> news:W-ednUfe0eT3P3_bnZ2dnUVZ_tajnZ2d@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.
jstucklex@attglobal.net
==================

Re: Download code

am 10.09.2007 17:46:49 von MKatz528

> 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.

Re: Download code

am 10.09.2007 19:36:23 von Jerry Stuckle

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.
jstucklex@attglobal.net
==================