You are here: Re: Download code « PHP Programming Language « IT news, forums, messages
Re: Download code

Posted by shimmyshack on 09/08/07 11:09

On Sep 8, 4:35 am, "Sheldon Glickler" <sheldo...@asap-consult.com>
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;
}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация