Page 1 of 1

[SOLVED!]Help with fopen()

Posted: Mon Oct 24, 2005 2:18 pm
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!

Posted: Mon Oct 24, 2005 3:47 pm
by hawleyjr
yes, the header function is what is forcing the download. Take a look at fopen copy and fwrite. All PHP Functions

Posted: Tue Oct 25, 2005 9:15 am
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.

Posted: Tue Oct 25, 2005 3:47 pm
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.

Posted: Tue Oct 25, 2005 3:49 pm
by feyd
fopen() and its siblings... as hawleyjr already stated..

Posted: Thu Oct 27, 2005 9:18 am
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'?

Posted: Thu Oct 27, 2005 9:47 am
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.

Posted: Thu Oct 27, 2005 10:26 am
by $var
thanks grim...!
you do rock!

this is perfect.