Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Newlines removed with fpassthru() for txt files?
Old 02-22-2009, 07:16 PM Newlines removed with fpassthru() for txt files?
Junior Talker

Posts: 1
Trades: 0
Hi all,

I read online about a way to "force" a file download dialog box using the Content-Disposition header combined with fpassthru() in PHP, like so:

(query string is to be specified as a filename - without path - to a file in the current working directory)

Code:
$filename = getenv('QUERY_STRING');
$stuff = preg_split( "/\./", $filename );
$ext  = array_pop( $stuff );
$mime = array(
    "html" => "text/html",
    "htm"  => "text/html",
    "txt"  => "text/html",
    "rtf"  => "application/rtf",
    "doc"  => "application/msword",
    "zip"  => "application/zip",
);
header( 'Content-Type: ' . $mime[ strtolower( $ext ) ] );
header( "Content-Disposition: attachment;filename=$filename" );
$h = fopen( $filename, 'r' );
fpassthru( $h );
fclose( $h );
exit();
It works fine, but for some reason whenever it passes a text file (.txt), all the newlines are removed after downloading it. doc/html/htm/zip files download flawlessly.

Anyone offer some insight into this, particularly how to transfer txt files properly?

Thanks in advance
kevinmd88 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-23-2009, 02:04 AM Re: Newlines removed with fpassthru() for txt files?
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Because you have:
"txt" => "text/html"
which should be:
"txt" => "text/plain"
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Newlines removed with fpassthru() for txt files?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.43882 seconds with 12 queries