Riparian Posted November 14, 2017 Share Posted November 14, 2017 HiI am Seeking help on exporting mysql files to php7 to make a labels programMy 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 <?phprequire_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 contentsecho $contents;exit;?><?phpfunction 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 Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 14, 2017 Share Posted November 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
Riparian Posted November 15, 2017 Author Share Posted November 15, 2017 Hi and thank you for your reply. Apologies for the code layout Actually the message shows only in excel so there is no way to capture the error that I can think of Please see this link for clarification goo.gl/zBfRvF Cheers Quote Link to comment Share on other sites More sharing options...
kicken Posted November 15, 2017 Share Posted November 15, 2017 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; Quote Link to comment Share on other sites More sharing options...
Barand Posted November 15, 2017 Share Posted November 15, 2017 Instead of "php://temp" use "php://output" then you can use fputcsv directly to the browser (STDOUT) and cut out the intermediate file writing/reading steps Quote Link to comment Share on other sites More sharing options...
Riparian Posted November 15, 2017 Author Share Posted November 15, 2017 hello kicken You are a legend... thank you so much for this fix ! this one thing has been holding me up for weeks since upgrading from 5.6 to 7. Cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.