CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Output cgi as .txt extension



Apr 10, 2008, 4:51 PM

Post #1 of 4 (234 views)
Output cgi as .txt extension Can't Post

Hi all,

I searched the FAQ and general board, but can't find the answer to this.

I have written a script that allows users to upload a log file (which I store to a mysql database as plain text). I then use another script that removes some predefined lines (by way of a foreach loop that replaces lines by regex match. Not efficient, but works).

The final aspect is that if the "trimmed" log is still larger than, say 2000 lines, I don't want it to display in a browser but rather give the users a link to save the file. I have achieved this, but my script merely checks the output after regex processing and if over 2000 lines, it runs the script again but with a simple "Content-type: text/plain" header. The filename the users get in the save-as dialog is myscriptname.cgi.

I would like for them to get processedfile.txt. I don't want to save the text on the server, as that will get very large as users may run the script with different options.

Any suggestions (or can I make this more clear, in case I've muddled my explanation)?



Apr 10, 2008, 11:18 PM

Post #2 of 4 (232 views)
Re: [chaotic] Output cgi as .txt extension [In reply to] Can't Post

When you say "save" the file, do mean the user should be able to download the file? Does your perl script write the proper headers for a file download? "Content-type: text/plain" is for display, not download.

If the page has already printed a complete http header you will have to redirect the user to download the file because the script will have to print a new set of headers that forces a file download.

Your best bet will probably be to save the file to disk if it is over 2K lines and after the file is downloaded delete it. If the user does not complete the download process you will have to clean up the temp file next time the script is run or run a cron job to clean out the temp files at set intervals so they don't pile up.


Apr 11, 2008, 11:15 AM

Post #3 of 4 (225 views)
Re: [KevinR] Output cgi as .txt extension [In reply to] Can't Post

Thanks for the suggestion.

The temp file is good, but I'm not sure about implementation. The problem is a bit complex to me:

What I have right now is that if over 1999 lines, it runs a simplified script that writes the following:

#Print the headers 
print "Content-type:text/text\n\n";
print "Content-Disposition: attachment; filename=trimmed.txt\n\n";
# then there's a bunch of regex replacements in a foreach loop
#print new file array
print @lines;

The server is outputting the data using chunked encoding, and when I take a packet capture I don't see the content-type nor content-disposition headers. But the browser is using them, because if I change it to text/html, it doesn't open in my text editor.

I can probably output @lines to a text file, but how would I go about deleting the file? The filename can't be the same each time the script is run, as it would stomp other user output files. And if I name it the same as the unique $id that each SQL entry has, I won't be able to get away with deleting the file later.

I don't have the ability to create a cron job on this server, so is there another way I can achieve this?

Thanks again for all the help!


Apr 11, 2008, 3:04 PM

Post #4 of 4 (224 views)
Re: [chaotic] Output cgi as .txt extension [In reply to] Can't Post

for the file download header:

print "Content-length: $filesize\r\n"; 
print "Content-Type: application/x-download\r\n";
print "Content-Disposition: attachment;filename=\"$file\"\r\n\r\n";

where $filesize is the size of the file returned by stat (or -s file test operator) and #file is the name you want the file to have.

You can give the file anyname when saving to disk, some random name or the value of time, but use $file to give the download file the proper name. Or just hardcode it into the header. After the file download is complete use the unlink() function to delete the file.

If you already have the file stored in an array you can avoid writing it to disk and just download the array (I think). But I am not sure how you would get the filesize, if you can't, you can just not use that part of the header but the download file dialog box will not know the filesize or show the progress bar as a file is downloaded. It may just say something like "unknown size", each browser is free to handle that however it wants.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives