save excel file to server using php

15,575

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);
Share:
15,575
arok
Author by

arok

Updated on June 04, 2022

Comments

  • arok
    arok almost 2 years

    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?

  • arok
    arok about 10 years
    No, it is not exist. The function "file_put_contents" should create the file if it desn't exist.
  • Mark Baker
    Mark Baker about 10 years
    If you're trying to save to a directory that file_put_contents() doesn't have permission to write to, such as /pricelists (remembering that this is from the filesystem root - /), would you expect it to ignore those permissions and still write the file? creating a directory if it didn't exist? - What price security?
  • arok
    arok about 10 years
    the permission for this folder "pricelists" is 777
  • Kev
    Kev about 10 years
    @arok - yes that's all very well, but are you expecting to write to a folder called pricelists within your website or on the root of the filesystem?