LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2007, 01:54 PM   #1
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
HTML code to use to save a downloaded file with an other name that the URL


Hi All,

On a web page I have a link to a file. This file is a temporary file with a unique random file name, like mr_station_2ckAZW.csv.

The idea is that when the user clicks on this file name he is presented the usual dialog "open or save the file"

Now when he chooses to save the file, he must edit the file name to something which is sensible, like mr_station_july_2007.csv.

In my PHP script I do know this file name which the user is most likely to use, but on the other hand I am stuck with the real name of the file.

The reason for that is that when the user enters the page, the PHP script generates the CSV file, and stores it as a temporary file. I cannot use a single filename because every user entering this page will generate it. If I use the same file name, the file can be overwritten while it is viewed by someone else etc. Neither can I assign a sensible name to the file like : july_2007, because there are an infinite number of options to generate the file. I cannot allow that user2 downloads the file which was just generated by user1 etc.

Summarizing, if I have a file XXX.CSV and the downloads it, can the default filename to save it be YYY.CSV?

I use Apache, PHP and Javascript. Since it is just "nice to have" I don't want to add another requirement to the enveronment like Java or so.

jlinkels
 
Old 06-13-2007, 08:24 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

I think you can't do that with HTML, but PHP can do it:
PHP Code:
<?php
header
("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"yadayada.txt\"");
echo 
"Yada\nYada\n";
?>
The name of the PHP script doesn't matter to the client - it will be "yadayada.txt".

Hope this helps.
 
Old 06-13-2007, 09:36 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Smile

Man!

This is EXACTLY what I needed! It also solves my next problem: "How To Generate A PDF Document On Demand After Clicking On A Link And Download It Without Leaving The Page"

I had seen the header function in PHP but I never comprehended that it should be used in this way.

Thanks a lot!

jlinkels
 
Old 06-21-2007, 07:28 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
An important addition to this.

I did exactly as Guttorm pointed out. It worked like a charm for CSV files in both Opera and IE.

Then I continued with the project and tried to download a dynamically generated PDF file. Saving or opening in Opera was no problem again, but in IE I could only save. Trying to open would download the file all right, but Acroread does not open te file and gives an error message "file not found".

I copied this piece of code from the PHP manual:

Code:
<?php
$mm_type="application/octet-stream";

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($url)) );
header('Content-Disposition: attachment; filename="'.basename($url).'"');
header("Content-Transfer-Encoding: binary\n");
                   
$fp = fopen($url, 'rb');
$buffer = fread($fp, filesize($url));
fclose ($fp);
                   
print $buffer;
?>
(The code is on the manual page of the "header" function on php.net and posted by rich at pioneering-principles dot co dot uk.)

Now don't ask me which line exactly causes the opening to work. I was in a hurry (who isn't?) and I was happy enough to have it working.

Opening the file in IE 6 works now, and it still works in Opera.

jlinkels

Edit: PS how do you guys get that nicely colored PHP syntax posted here?
 
Old 06-21-2007, 07:40 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
One more thing:

The call to the header function header should generate the very first output of a web page. Even a <html> tag or a space is not allowed.

This is especially tricky when you include some other PHP files. They might output some innocent code which you do not normally see on a web page (like <strong></strong>)

The recommended method for avoiding this is to call this function:

Code:
<?
ob_start();
--- blah blah blah and other output ---
ob_end_flush();
header("Content-Type: text/plain"); 
header("Content-Disposition: attachment; filename=\"yadayada.txt\"");
?>
Now the problem her is that when you call ob_end_flush, everything which was nicely buffered is sent to your browser and picked up by the save function.

So instead of the wanted file, you receive all that buffered output that you didn't want in the first place! It really screws up the downloaded file.

So it is essential that you do:
Code:
ob_start();
--- blah blah blah and other output ---
ob_end_clean();
ob_end_flush();
to clean the buffer before you start sending downloaded files!

jlinkels
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
suggestions on how to code a program that would edit and save a text file tinieprotonjam Programming 5 01-28-2007 06:28 AM
[Perl] cgi.pm - save input in .html file noir911 Programming 2 01-07-2007 02:36 PM
Unable to save files downloaded from the internet on my user account chreon Linux - General 6 03-09-2006 04:05 AM
html code and including html files Hockeyfan Programming 2 08-22-2005 05:11 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration