Attachment Script
Moderator: General Moderators
Attachment Script
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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
use mime-content-type with some conditionals in conjuction with readfile
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 );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.Todd_Z wrote:Code: Select all
echo readfile( $file );
Also, is there such a a Content-Type as image?
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 );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 );Here is my version: http://timvw.madoka.be/programming/php/download.txt.
You should not echo the Content-Disposition, but use header...
You should not echo the Content-Disposition, but use header...
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...
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 );