Jump to content

export excel fails in php7


Riparian

Recommended Posts

Hi

I am Seeking help on exporting mysql files to php7 to make a labels program

My code has worked for many years but now breaks with php7 Now, when I run the program all I get is "cannot open protected file"

 

I have tired a number of different headers to no avail

This is the code that produces the excel file. the importing of records into the database all works fine. it seems just the export to exce that has changedl


<?php
require_once('../includes/session.php');

// Get data records from table.

$Result=mysqli_query($GLOBALS["___mysqli_ston"], "select * from apo_bulk  ") or die ('482.....'.mysqli_error($GLOBALS["___mysqli_ston"]));

//go get the data we need...
//$Result=mysql_db_query($DBName,$finalSQL,$Link);
//fetching each row as an array and placing it into a holder array ($aData)
$x=0;
while($row = mysqli_fetch_assoc($Result)){
 $x++;
$aData[] = $row;
if($x==1){
$aData[] =array('A','0.5','17','10','6');
$x=0;
}

}

$contents = getExcelData($aData);

$filename = "apo_label.xls";

//prepare to give the user a Save/Open dialog...
header ("Content-type: application/octet-stream");
header ("Content-Disposition: attachment; filename=".$filename);

//setting the cache expiration to 30 seconds ahead of current time. an IE 8 issue when opening the data directly in the browser without first saving it to a file
$expiredate = time() + 30;
$expireheader = "Expires: ".gmdate("D, d M Y G:i:s",$expiredate)." GMT";
header ($expireheader);

//output the contents
echo $contents;
exit;
?>
<?php
function getExcelData($data){
$retval = "";
if (is_array($data)  && !empty($data))
{
$row = 0;
foreach(array_values($data) as $_data){
if (is_array($_data) && !empty($_data))
{
if ($row == 0)
{

###### REMOVED BECAUSE WE DO NOT WANT THE FIRST LINE TO BE HEADER NAMES
// write the column headers
//   $retval = implode("\t",array_keys($_data));
// $retval .= "\n";
}
//create a line of values for this row...
$retval .= implode("\t",array_values($_data));
$retval .= "\n";
//increment the row so we don't create headers all over again
$row++;
}
}
}
return $retval;
}

 

 

?>

 

Any help is appreciated.

Cheers

Link to comment
Share on other sites

First of all, you should wrap your code between CODE tags, so it becomes easier to read and understand.

 

Secondly, you should tell us at which line this error is displayed (since there's no custom error message "cannot open protected file" in your code, most likely it comes right from PHP. If it's true, PHP always displays number of line where problem occurs.

 

Once you complete these steps, I'm sure we will be able to assist you further.

Link to comment
Share on other sites

Unless you're going to be producing an actual excel file, don't use the .xls extension.

 

You appear to be trying to output a tab separated file currently. Comma-separated files are more typical and would likely be supported better, so I'd suggest you use that instead. You'll want to use .csv for your extension and the text/csv mime type then.

 

To format your data correctly for a CSV file, you can use the fputcsv function. Open a temporary csv file, write your data to it then read it out to the browser.

 

$fp = fopen('php://temp', 'w+');
while($row = mysqli_fetch_assoc($Result)){
    fputcsv($fp, $row);
    fputcsv($fp, array('A','0.5','17','10','6'));
}

//prepare to give the user a Save/Open dialog...
header ("Content-type: text/csv");
header ("Content-Disposition: attachment; filename=apo_label.csv");

//setting the cache expiration to 30 seconds ahead of current time. an IE 8 issue when opening the data directly in the browser without first saving it to a file
$expiredate = time() + 30;
$expireheader = "Expires: ".gmdate("D, d M Y G:i:s",$expiredate)." GMT";
header ($expireheader);

//output the contents
rewind($fp);
fpassthru($fp);
fclose($fp);

exit;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.