Modify Text File Download Without Modifying Saved File?

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
wft
Forum Newbie
Posts: 3
Joined: Wed Sep 30, 2009 8:13 am

Modify Text File Download Without Modifying Saved File?

Post by wft »

Hi. I'm new to the forum. I have a rather unusual need, and I'm seeking ideas on how to fulfill it.

I want to write a PHP script that will take an existing text file and offer it as a downloadable link on a web page. Simple enough, except that when the user clicks the link to download it, I want the file to automatically get modified on the fly before delivering it to the user. but without any changes being made to the original file stored on the server. In other words, I want to do something similar to what can be done with an image, like adding text to the image (or some other modification) before delivering it to the user; instead of modifying an image, I want to modify a .txt file (or .mobi, or .pdf, or .rtf, etc.)

Any ideas?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Modify Text File Download Without Modifying Saved File?

Post by jackpf »

Umm....read the file, make the changes, and then send it to the user? What are you having trouble with? :/
wft
Forum Newbie
Posts: 3
Joined: Wed Sep 30, 2009 8:13 am

Re: Modify Text File Download Without Modifying Saved File?

Post by wft »

jackpf wrote:Umm....read the file, make the changes, and then send it to the user? What are you having trouble with? :/
Forgive me if I'm overlooking something obvious. My understanding (probably wrong) is that you normally have to write the changes to the stored file before allowing the user to download it. I'm not used to handling "in stream" changes, which is why I'm asking for help.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Modify Text File Download Without Modifying Saved File?

Post by jackpf »

Well, to download a file, you send all the headers...and then send the file contents right?

So, make a script, which sends all the headers, loads up the file, makes whatever changes it wants, and then sends the file.

Have a look at fopen() or file_get_contents() or something similar.
wft
Forum Newbie
Posts: 3
Joined: Wed Sep 30, 2009 8:13 am

Re: Modify Text File Download Without Modifying Saved File?

Post by wft »

Yes, of course. Thanks for your help.
Post Reply