Page 1 of 1
Attachment Script
Posted: Thu Nov 03, 2005 4:13 pm
by Todd_Z
I have an attachment page, that depending on the input variable, which is a reference number to a certain attachment, it should either display it if its a text file, or display the image if its an image. Any ideas on how to do this?
Posted: Thu Nov 03, 2005 5:29 pm
by John Cartwright
use
mime-content-type with some conditionals in conjuction with
readfile
Posted: Thu Nov 03, 2005 7:07 pm
by Todd_Z
Thanks Jcart - what do you think of the result?
Code: Select all
<?
define( "HOME", "/home/XXXXXXXXXXXX/" );
$files = scandir( HOME."info/attachments/" );
foreach ( $files as $file )
if ( preg_match( "#^{$_GET['id']}-([^\.]*)\.(.*)$#", $file, $fn ) ) {
$attachment = $fn[1];
$ext = $fn[2];
break;
}
if ( !$attachment )
die( "Invalid Attachment ID" );
$file = HOME."info/attachments/{$_GET['id']}-$attachment.$ext";
function get_mime_type ( $f ) {
$f = escapeshellarg( $f );
return trim( `file -bi $f` );
}
$mime = get_mime_type( $file );
preg_match( "#^([^/]*)/(.*)$#", $mime, $info );
switch ( $info[1] ) {
case "text":
header( "Content-type: text/plain" );
break;
case "image":
header( "Content-type: $mime" );
break;
default:
header( "Content-Type: application/octet-stream" );
break;
}
echo readfile( $file );
Posted: Thu Nov 03, 2005 7:12 pm
by redmonkey
readfile() reads a file and throws it directly to the buffer, it also returns the number of bytes read, so that line you have there will corrupt the download files.
Also, is there such a a Content-Type as image?
Posted: Thu Nov 03, 2005 7:18 pm
by Todd_Z
Code: Select all
switch ( $info[1] ) {
case "text":
case "image":
header( "Content-type: $mime" );
break;
default:
header( "Content-Type: application/octet-stream" );
break;
}
readfile( $file );
Change: text and image just throw the respective mime type, everything else is downloaded.
Posted: Thu Nov 03, 2005 7:42 pm
by Todd_Z
why isn't this working? It's ignoring the filename completely.
Code: Select all
switch ( $info[1] ) {
case "text":
case "image":
header( "Content-type: $mime" );
break;
default:
header( "Content-Type: application/octet-stream" );
echo( "Content-Disposition: attachment; filename=\"{$attachment}.{$ext}\"" );
break;
}
readfile( $file );
Posted: Thu Nov 03, 2005 7:46 pm
by redmonkey
Disposition and filename are only set in the 'default' case.
Now, correct me if I'm wrong but the text mine type is actually text/plain and image mine types are image/'type' where 'type' is the image type i.e. png, gif or jpeg?
Posted: Thu Nov 03, 2005 7:47 pm
by timvw
Here is my version:
http://timvw.madoka.be/programming/php/download.txt.
You should not echo the Content-Disposition, but use header...
Posted: Thu Nov 03, 2005 10:27 pm
by Todd_Z
Thanks timvw,
I'd like to ouput php files as highlighted_file(), but it seems that php files are returning a mime of text/plain...
Code: Select all
function get_mime_type ( $f ) {
$f = escapeshellarg( $f );
return trim( `file -bi $f` );
}
$size = filesize( $file );
$mime = get_mime_type( $file );
header( "Pragma: public" );
header( "Expires: 0" );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Cache-Control: public" );
header( "Content-length: $size" );
header( "Content-type: $mime" );
preg_match( "#^([^/]*)/(.*)$#", $mime, $info );
if ( !in_array( $info[1], array( "image", "text" ) ) )
header( "Content-Disposition: attachment; filename=$attachment.$ext" );
readfile( $file );