#1 (permalink)  
Old 03-22-2006, 12:04 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
Creating a file and saving it locally - PHP

Okie dokie.

I've figured out how to create a file and store it in a directory on my web server, but I cannot figure out how to generate a file that can easily be saved locally by the user.

I would like the user to be able to select in which directory they save the new file.

I thought about using an <input type='file'> sort of thing, but that requires you to select an actual, existing file. I can't figure out how to get it to allow you to simply choose a directory.

The file I am generating is a txt file.

Any help would be appreciated.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote

  #2 (permalink)  
Old 03-22-2006, 06:55 PM
Till's Avatar
Administrator
 
Join Date: Jan 2002
Location: Berlin, Germany
Posts: 1,453
You can't save on the user's computer. You can provide a link, and make "download box" appear. That is all!
Reply With Quote
  #3 (permalink)  
Old 03-22-2006, 07:05 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
Quote:
Originally Posted by Till
You can't save on the user's computer. You can provide a link, and make "download box" appear. That is all!
That's basically what I meant.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #4 (permalink)  
Old 03-22-2006, 08:44 PM
Till's Avatar
Administrator
 
Join Date: Jan 2002
Location: Berlin, Germany
Posts: 1,453
Quote:
Originally Posted by curtiss
That's basically what I meant.
Here is some ultra-secret code from the HTMLCenter labz!

PHP Code:
// this is the path to the file you want to download
$file_name 'foobar.txt';
$file_to_download='/path/to/' $file_name;

@
header("Pragma: public");
@
header("Expires: 0");
@
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
@
header("Content-Type: application/octet-stream");
@
header("Content-Disposition: attachment; filename=\"$file_name\"");
@
header("Content-Description: File Transfert");
@
readfile($file_to_download); 
Reply With Quote
  #5 (permalink)  
Old 03-23-2006, 08:04 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
Awesome. Thank you Till. I'll give it a shot.

I'm guessing I have to obviously generate the file and store it on my server before running this code?
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #6 (permalink)  
Old 03-25-2006, 11:56 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
Hmm. I just tried it out, and, for some reason, rather than prompting the user to save/open the file, it simply included the content of the file ahead of the content on my page.

It's actually a dump of an SQL database, so I tried saving it as an SQL file instead of a txt file, and wound up with the same result. Rather than prompting the user to download the file, it simply prints the content of the file at the top of the page.

Is there something I can change in the header information in order to make the browser realize I want the user to be prompted for open/save, rather than simply printing the content?

If not, I guess I'll have to figure out how to dynamically archive the file into a tar archive or something, and then open the file.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #7 (permalink)  
Old 03-25-2006, 12:26 PM
Till's Avatar
Administrator
 
Join Date: Jan 2002
Location: Berlin, Germany
Posts: 1,453
I am using the above code to force downloading of MP3 files. And it works. Not sure what else, you'd have to do. If you prompt for a .sql file and your own browser's settings tell it to open it (you can save this option when you selected it), it won't work.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 04:21 PM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter