Attachment Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Attachment Script

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

use mime-content-type with some conditionals in conjuction with readfile
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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 );
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Todd_Z wrote:

Code: Select all

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.

Also, is there such a a Content-Type as image?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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 );
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Here is my version: http://timvw.madoka.be/programming/php/download.txt.

You should not echo the Content-Disposition, but use header...
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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 );
Post Reply