[SOLVED!]Help with fopen()

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
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

[SOLVED!]Help with fopen()

Post by $var »

I have this script to export a form to an Excell Database... but all I can find is a way to make it prompt the user to download to their computer...
This appearantly isn't proper, and needs to be send to a spot on our server, without prompt.

Code: Select all

<? $select = "SELECT * FROM surveys WHERE Mem_ID =".$ID;                
		$export = mysql_query($select);
		$fields = mysql_num_fields($export);
			for ($i = 0; $i < $fields; $i++) {
		    $header .= mysql_field_name($export, $i) . "\t";
			while($row = mysql_fetch_row($export)) {
				$line = '';
				foreach($row as $value) {                                            
					if ((!isset($value)) OR ($value == "")) {
						$value = "\t";
					} else {
						$value = str_replace('"', '""', $value);
						$value = '"' . $value . '"' . "\t";
					}
					$line .= $value;
				}
				$data .= trim($line)."\n";
			}
			}
			$data = str_replace("\r","",$data); 
			
			if ($data == "") {
		    $data = "\n(0) Records Found!\n";                        
			} 
			$xlsdate = date('Y-m-d'); 
			header("Content-type: application/x-msdownload");
			header("Content-Disposition: attachment; filename=$xlsdate$ID.xls");
			header("Content-Disposition: attachment; filename=$xlsdate$ID.xls");					
			header("Pragma: no-cache");
			header("Expires: 0");
			print "$header\n$data"; 
?>
I'm certain that it would be in these lines...
What would you do to select a location to automatically download this file, prompt free... on the sly if you will.

Code: Select all

header("Content-type: application/x-msdownload");
			header("Content-Disposition: attachment; filename=$xlsdate$ID.xls");
			header("Content-Disposition: attachment; filename=$xlsdate$ID.xls");
THANK YOU!
Last edited by $var on Thu Oct 27, 2005 10:39 am, edited 2 times in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

yes, the header function is what is forcing the download. Take a look at fopen copy and fwrite. All PHP Functions
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

sadly, i don't understand this.

fopen copy and fwrite all functions...
i'm guessing by the name and sequence,
that this opens the file, copies it, and writes it to the server... or something to that effect.

could you give me an example of what this might look like, maybe not for all the functions, but for at least one, so that i can work from that?
i know that that is asking a lot, but i bet that it would really help me grasp this idea.

thanks for your response.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

Okay, So investigation on what these mean, is that I am saying, yes, please prompt me to download this as a file.
From above, I know that I should be printing this, but I tried a few different methods... however, I can't make it happen.

When I remove

Code: Select all

header("Content-Disposition: attachment; filename=$xlsdate$ID.txt");
		    header("Content-Disposition: attachment; filename=$xlsdate$ID.txt");
It exports the survey.php that it's on, as tab delimited, which is great...

Code: Select all

header("Content-type: application/x-msdownload");
		    header("Pragma: no-cache");
		    header("Expires: 0");
		    print "$header\n$data";
I made a $path to where I want the file to dump, and a root for it as well.

Code: Select all

$file =  "f:/www/vhosts/tab.com/httpdocs/access/02members/survery/submit/$xlsdate$ID.txt";
		$document_root =  "f:/www/vhosts/tab.com/httpdocs/access/02members/survery/submit/";
I just don't know how to get it there... please, please, please, if you have any suggestions i'd love to try them.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fopen() and its siblings... as hawleyjr already stated..
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

reading the manual about fopen, copy, and fwrite...
the are all based on a file that prexists, or like, a directory.

the example given in the manual is this

Code: Select all

$handle = fopen("/home/rasmus/file.txt", "r");
I'm confused, because I need to create a file from submitted database information.
Which file would I be opening with fopen()?
Wouldn't I need to employ a script that makes the file?
Is that using the 'x' mode instead of 'r'?
Last edited by $var on Thu Oct 27, 2005 9:48 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

http://www.phpnoise.com/tutorials/20/3

If you can't get that to work:
Have a blank template, copy it with copy(), the use fwrite() to do whatever you want.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

thanks grim...!
you do rock!

this is perfect.
Post Reply