|
Posted by The Natural Philosopher on 09/08/07 10:51
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.jpg</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
>
>
Navigation:
[Reply to this message]
|