arok
arok

Reputation: 1845

save excel file to server using php

I am retrieving data from database and exporting it to excel with the below code, from this i can export to excel and saving the file from browser.

$sql_select= "my query";
$queryRes = mysql_query($sql_select);

$header = "Country" . "\t";
$header .= "Network" . "\t";
$header .= "MCC" . "\t";
$header .= "MNC" . "\t";
$header .= "ClientPrice" . "\t";
$header .= "Currency" . "\t";

$data = '';
while( $row = mysql_fetch_assoc($queryRes)){
$row1 = array();
$row1[] = $row['country'];
$row1[] = $row['networkname'];
$row1[] = $row['mcc'];
$row1[] = $row['mnc'];
$row1[] = $row['clientprice'];
$row1[] = $row['currency'];
$data .= join("\t", $row1)."\n";
//$data= $first_name."\t";
//$data .= $row['originator']."\t";
}

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=expot.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; 
exit(); 

I am using the below code to save the excel file to this location "/pricelists/example.xls"

file_put_contents("/pricelists/example.xls",$data);
print "$header\n$data";

It is showing warning

Warning: file_put_contents(/pricelists/example.xls) [function.file-put-contents]: failed to open stream:

Can anyone guide me how to fix this thanks?

Upvotes: 3

Views: 18724

Answers (2)

Alyas
Alyas

Reputation: 630

Does your file exists at the path /pricelists/example.xls?

Using relative path to the file may solve the issue.

file_put_contents("pricelists/example.xls",$data);

Upvotes: 3

miny1997
miny1997

Reputation: 478

If you use Windows:

Go to the "Properties" of your folder containing the output file, disable read-only, go to the "Security" index, click on "Edit" and then on "Add", type "Everyone" and give full permissions and apply the settings.

If you use Linux or you have an hired webserver:

Give the full permission with CHMOD 777 /your/folder/ or with your FTP application.

Upvotes: -1

Related Questions