saving file on the user's computer

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
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

saving file on the user's computer

Post by TheStoryTeller »

Hello, I need to process some data and save it. I am forbidden to save it on my own database - so I will not.
Is there a way to save a file on the user's comptuer (forget about how I can get it back for now)?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: saving file on the user's computer

Post by Darhazer »

How much data you need to save? If it is small amount of data, you can use cookie. You can save a file also just asking the user to download it... you just need to send the correct headers and output the data... the browser will ask the user to save the file and will save it. Then you have to ask the user to upload the file in order to load the data back.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: saving file on the user's computer

Post by jaoudestudios »

TheStoryTeller wrote:Is there a way to save a file on the user's comptuer
Isnt that what viruses do?
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

Re: saving file on the user's computer

Post by TheStoryTeller »

Darhazer wrote:How much data you need to save? If it is small amount of data, you can use cookie. You can save a file also just asking the user to download it... you just need to send the correct headers and output the data... the browser will ask the user to save the file and will save it. Then you have to ask the user to upload the file in order to load the data back.
ok, both methods sound good, I think I like the first one better. The data i'm collecting is a simple list (probably text file in a certain format or it could be XML) but it's size can be between 5 to 20 KB, depends on the user. is that range of size is "cookie-able"?
and about the headers.. I never really got them down propely.. if your'e aware of any good tutorial/example on the subject (other than the php.net site..) which i'll miss if I simply google it - I would love to know about it.
Thank You.
TheStoryTeller
Forum Newbie
Posts: 15
Joined: Fri May 01, 2009 10:13 am

Re: saving file on the user's computer

Post by TheStoryTeller »

jaoudestudios wrote:
TheStoryTeller wrote:Is there a way to save a file on the user's comptuer
Isnt that what viruses do?
hmmm.. but I don't want to build a virus.. are you implying that it's impossible? or maybe I need the user's permission?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: saving file on the user's computer

Post by jaoudestudios »

you will need the users permission, unless you use a cookie as Darhazer suggested, but then you are limited with how much data you can save.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: saving file on the user's computer

Post by Darhazer »

More than 4 KB is not suitable for cookies, altough some web browsers and/or web server can allow it.

As for the download, I have written such few times, I have no code in front of me (I can send one when I'm in the office, just P.M. me), but generally you need Content-type: application/ocstream, and Content-dispossition: attachment.
I think using this keywords you can find a working example :)

Also, you may be interested in how much data can flash store in the SharedObject, if flash is suitable for your needs. The last suggestion is going trough ActiveX control - if user permit it, it have access to the filesystem.

And one more think - you can try using a service to save the data on a web host, and set only id in the cookie, or switch the hosting provider, so you can save at the server.

Good luck!
Post Reply