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
Rename file downloaded with fopen and fpassthru
Old 01-12-2010, 04:59 PM Rename file downloaded with fopen and fpassthru
Skilled Talker

Posts: 61
Trades: 0
I have a script that allows subscribers to download a monthly PDF. The PDFs are stored in a non-public directory, so I use the following script to get the files to them:

PHP Code:
$fileName mysql_result($resIs,0,"fileName");
        
$fp fopen($path .$fileName"r");
        
header("Content-type: application/pdf");
        
fpassthru($fp);
        
fclose($fp); 
So based on the download link that has been generated, it looks up the issue number to allow the download of the correct file.

For most users, this works fine - their browsers automatically open the file in their PDF reader. However, it downloads a file called "download.php" (which is the php file the above code is in) rather than say, "issue65.pdf" which is the actual file. They can manually rename download.php to a file name with a .pdf extension and it works fine, but I would like to fix this so the script just downloads a properly named pdf file, and I don't have the wherewithall to do so.

Any suggestions? Thanks.
gregory is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-12-2010, 11:16 PM Re: Rename file downloaded with fopen and fpassthru
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
This is taken directly from the examples of php.net (of the header() function).

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.
PHP Code:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 01-12-2010 at 11:18 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 01-14-2010, 01:23 PM Re: Rename file downloaded with fopen and fpassthru
Skilled Talker

Posts: 61
Trades: 0
thanks, works great
gregory is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Rename file downloaded with fopen and fpassthru
 

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.11679 seconds with 12 queries