Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Save As..." dialogue box 1

Status
Not open for further replies.

tchev

Programmer
Dec 20, 2001
3
GB
Does anyone know how to get the standard Windows "Save As..." dialogue box to pop up?

I'm using PHP to create a comma seperated file, generated from a MySQL database. I now need to prompt the user to save this file to a location on their computer.

I can force the file onto a users C: drive using the following code, but this doesn't strike me as being particularly elegant.

$file_string = date("dmy_Hi") . ".txt";

$db = mysql_connect("server", "root");
mysql_select_db("user", $db);

@$sub = fopen("c:/" . $file_string,"w");
if (!$sub) echo "error creating c:/" . $file_string;

@$result = mysql_query("SELECT * FROM table", $db);
$column_number = mysql_num_fields($result);
if (!$column_number) echo "error reading table header";

unset($header);
for($i=0 ; $i<$column_number ; $i++) $header[$i] = mysql_field_name($result, $i);
fputs($sub,&quot;'&quot; . implode(&quot;','&quot;,$header) . &quot;'\r\n&quot;);

while($data = mysql_fetch_array($result))
{
unset($to_file);
for($i=0;$i<$column_number;$i++)
{
$to_file[$i] = preg_replace(&quot;/\'/&quot;,&quot;''&quot;,stripslashes(trim($data[$header[$i]])));
}
fputs($sub,&quot;'&quot; . implode(&quot;','&quot;,$to_file) . &quot;'\r\n&quot;);
}
$msg = &quot;File &quot;. $file_string . &quot; succesfully downloaded to your C: drive.&quot;;
fclose($sub);


Any ideas appreciated.

[bomb]
 
From the code above, it looks like that would write a file to *YOUR* hard drive, and no-one elses.

Perhaps a page containing this code :

download.php------------------------------
<?php
if(file_exists($_GET[FileName])){
$size = filesize($_GET[FileName]);
header(&quot;Content-Type: application/save&quot;);
header(&quot;Content-Length: $size&quot;);
header(&quot;Content-Disposition: attachment; filename=$_GET[FileName]&quot;);
header(&quot;Content-Transfer-Encoding: binary&quot;);
$fh = fopen(&quot;$_GET[FileName]&quot;, &quot;r&quot;);
fpassthru($fh);


}else{

echo &quot;No file selected&quot;;

}

?>

and then make a link <a href=download.php?FileName=file_to_be_downloaded>download file</a>

Hope this helps. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Cheer KarveR - bit of a schoolboy error from me there I'm afraid!

Your code seems to do exactly what I need.....except....

I can't seem to get the &quot;Save As&quot; dialogue box to populate with the $_GET[FileName] string. It always populates the filename box with the page name, ie: download_2.

This isn't a major problem, but it is really annoying me!

Am I missing something (again) ??
 
No, I've managed to successfully manage not to figure that bit out too, annoying as hell but it does the job anyway.
I'll post a star for anyone who can fix that bit ;-) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR,

Just wanted to let you (and anyone else who's interested) know that I've got around this problem by using a combination of our codes.

The following code is now working fine. It'll read from the database, throw all the data into a string ($file), and then prompt the user to download the file. Whatever you put in $file_name will appear in the &quot;File name&quot; box of the pop-up.

$file_name = date(&quot;dmy_His&quot;) . &quot;.txt&quot;;

$db = mysql_connect(&quot;server&quot;, &quot;user&quot;);
mysql_select_db(&quot;database&quot;, $db);

@$result = mysql_query(&quot;SELECT * FROM tables&quot;, $db);
$column_number = mysql_num_fields($result);
if (!$column_number) echo &quot;error reading table header&quot;;

$file = &quot;&quot;;

/*get column names*/
unset($header);
for($i=0 ; $i<$column_number ; $i++) $header[$i] = mysql_field_name($result, $i);
$file .= &quot;'&quot; . implode(&quot;','&quot;,$header) . &quot;'\r\n&quot;;

/*get data*/
while($data = mysql_fetch_array($result))
{
unset($to_file);
for($i=0;$i<$column_number;$i++)
{
$to_file[$i] = preg_replace(&quot;/\'/&quot;,&quot;''&quot;,stripslashes(trim($data[$header[$i]])));
}
$file .= &quot;'&quot; . implode(&quot;','&quot;,$to_file) . &quot;'\r\n&quot;;
}

header(&quot;Cache-Control:&quot;);
header(&quot;Content-Type: application/octet-stream&quot;);
header(&quot;Content-Length: &quot; . strlen($file));
header(&quot;Content-disposition: attachment; filename=&quot; . $file_name);
header(&quot;Content-Transfer-Encoding: binary&quot;);
echo $file;
exit();


Thanks again KarveR, I wouldn't have got anywhere near the answer without ya help.

Cheers [cheers]
 
Aha.. I see I had my quotes and strings not organised correctly.

As promised, one star awarded ;-) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top