简体   繁体   中英

excel automatically open file after downloading in php

below is my codes for my excel. its functioning except for one thing. i don't see if my file is already save to excel.. can you help me how to automatically open my file after i download it.. i think something is missing or wrong in my codes.. so please. help . thanks.

{

$conn = mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("copylandia",$conn);

//$fp = fopen($filename,"w+");

$filename = 'attachment'. date('Y-m-d') .'.csv';



$fp = fopen($filename,"w+");
$sql = mysql_query("select * from user") or die (mysql_error());
$num_rows = mysql_num_rows($sql);

if($num_rows >= 1)
{

$row = mysql_fetch_assoc($sql);

$fp = fopen($filename,"w+");
$seperator = "";
$comma = "";

foreach($row as $name => $value)
{

$seperator .= $comma .'' . str_replace('','""',$name);
$comma = ",";

}
$seperator .= "\n";

//echo $seperator;

fputs($fp,$seperator);


mysql_data_seek($sql, 0);


while($row = mysql_fetch_assoc($sql))
{
$seperator = "";
$comma = "";

foreach($row as $name => $value)
{

$seperator .= $comma .'' . str_replace('','""',$value);
$comma = ",";

}
$seperator .= "\n";

fputs($fp,$seperator);
}
fclose($fp);
}
else
{
echo 'No records in the database!';
}
}

You can't force the browser/computer to open a file after download. The user must make that decision.

Although, since you are trying to make a CSV file, I will at least make a suggestion to help you out instead of crushing your dreams by telling you you can't do this: Try fputcsv() instead of trying to make the string yourself: I'm really cool! Click me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM