PHPExcel not download or no process at all - php

i'm trying to export from mysql to excel using PHPExcel, i create a button to start exporting proses. It's loading but return with blank white page, i check every line from the code and didn't found any problem. Calling data from table is normal, check it with echo. so i think there is a problem with exporting process, here is the code
require_once 'PHPExcel/PHPExcel.php';
$excel=new PHPExcel();
$excel->getProperties()->setCreator('Flora Sitinjak')
->setLastModified('Flora Sitinjak')
->setTittle('Laporan Pendapatan')
->setSubject('Pendapatan')
->setDescription('Laporan Semua Pendapatan')
->setKeywords('Data Pendapatan');
$exp=$excel->setActiveSheetIndex(0);
$exp->setCellValue('A1', "Tanggal");
$exp->setCellValue('B1', "EKS");
$exp->setCellValue('C1', "BIS");
$exp->setCellValue('D1', "EKO");
$exp->setCellValue('E1', "L.EKO");
$exp->setCellValue('F1', "Total");
$line=2;
$x=0;
for($i=1;$i<=$tgl[11];$i++){
$exp->setCellValue("A".$line, $i);
$carTL=count($arTL);
$gTGLO=substr($arTL[$x],-2);
if(empty($gTGLO)){
$x=0;
}else{
if($i==$gTGLO){
if(($x+1)>=$carTL){
$x=$carTL-1;
$exp->setCellValue("B".$line, $arLUEKS[$x]);
$exp->setCellValue("C".$line, $arLUBIS[$x]);
$exp->setCellValue("D".$line, $arUEKO[$x]);
$exp->setCellValue("E".$line, $arLEKO[$x]);
$exp->setCellValue("F".$line, $arTot[$x]);
}else{
$exp->setCellValue("B".$line, $arLUEKS[$x]);
$exp->setCellValue("C".$line, $arLUBIS[$x]);
$exp->setCellValue("D".$line, $arUEKO[$x]);
$exp->setCellValue("E".$line, $arLEKO[$x]);
$exp->setCellValue("F".$line, $arTot[$x]);
$x++;
}
}else{
$exp->setCellValue("B".$line, '-');
$exp->setCellValue("C".$line, '-');
$exp->setCellValue("D".$line, '-');
$exp->setCellValue("E".$line, '-');
$exp->setCellValue("F".$line, '-');
}
}
$line++;
}
$excel->getActiveSheet()->setTitle('Laporan');
$excel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="Data Laporan Pendapatan.xlsx');
header('Cache-Control: max-age=0');
$write=PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$write->save('php://output');
Every array like $arLUEKS[$x] is called from database and works, checked with echo
The output is a table with shows data a month from 1 to 31, if the data doesn't exist then it show a -
Anyone can solve this? please help me

I do not know what exactly could be wrong but here are some helpful things you can do that can help you identify your problem .
Turn on error reporting by adding these two to the top of your script
ini_set("display_errors",1);
error_reporting(E_ALL);
This may point you to the cause. Maybe the script is timing out or something

Related

export mysql tables and zipArchive

I've searched all the posts, but still couldn't get it to work. With button press I want to export "page preset" to be precise it's restaurant menu preset, it includes css files, mysql tables etc. I want to be able to import it to another 'menu'. First I'm trying to export mysql database. Should I use mysqldump or SELECT * INTO OUTFILE ?
I'm using this line:
exec("mysqldump --user=$dbusername --password=$dbpassword restaurantsdb meal --where=restaurant_id=$restId > tables/meal.sql");
restaurantsdb is database name and meal = table name. I also want rows WHERE 'restaurant_id' = {id}
I'm trying to understand how mysqldump works, should this line work for me?
Next I'm creating zipArchive file, adding some directories, I tried adding .txt file, that one works, but it doesn't seem to find meal.sql file.
$zip = new ZipArchive();
if ( $zip->open($zip_file, ZipArchive::CREATE) !== TRUE) {
exit("error");
}
$zip->addEmptyDir('TestFiles');
$zip->addEmptyDir('tables');
$fileToZip = __DIR__.'/hello.txt';
$zip->addFile($fileToZip, "TestFiles/text.txt");
$fileToZip = __DIR__.'/tables/meal.sql';
$zip->addFile($fileToZip, "tables/meal.sql");
$download_file = file_get_contents( $file_url );
$zip->addFromString(basename($file_url),$download_file);
$zip->close();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zip_file).'"');
header("Content-length: " . filesize($zip_file));
header("Pragma: no-cache");
header("Expires: 0");
ob_clean();
flush();
readfile($zip_file);
unlink($zip_file);
exit;
Also, is it better to export as .sql or .csv ? Later when importing, I need to be able to change id's of all rows to specified, just before importing to database. It's basically cloning same data, but different id's.

Need Table headings added to exported CSV file

I have a script that takes a SQL Statement and puts the query result into a CSV file. Right now it only the rows of the table, I want it to Put the headings of the table at the top. How would I do that with this current script?
Script...
<?php
include 'connect.php';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment;filename="export.csv"');
header('Cache-Control: max-age=0');
$fpcsv = fopen('php://output', "a+");
$sqlstatement = $_GET['sqlstatement'];
$exportcsv_q = mysql_query($sqlstatement);
if (#mysql_num_rows($exportcsv_q) > 0) {
$campos = mysql_num_fields($exportcsv_q);
while ($exportcsv_r = mysql_fetch_row($exportcsv_q)) {
fputcsv($fpcsv, $exportcsv_r);
}
}
exit;
?>
If anyone out there has a mysqli version of this I would love to be able to switch this over.
First, the answer to your question: output the headers before your while loop:
if (#mysql_num_rows($exportcsv_q) > 0) {
// output headers here
fwrite($fpcsv, "header1,header2,foo,bar,..."); // <-- like this
// if you want, you could do it this way:
// fputcsv($fpcsv, array("header1", "header2", ...));
$campos = mysql_num_fields($exportcsv_q);
while ($exportcsv_r = mysql_fetch_row($exportcsv_q)) {
fputcsv($fpcsv, $exportcsv_r);
}
}
If you want to have the headers even if there is no data, just move that line outside your if block, as well.
Second, yes, you should stop using mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead. How to do that is too broad for a single question here. If you get stuck on some aspect of it, ask a new question about that.
Third, swallowing/suppressing errors with # is considered bad practice and can lead to unexpected results. Use a proper try { ... } catch (...) {...} block instead so you can handle/log/report the error as needed.

mysqli export to xls issue

I have troble geting this work and I can't figure it out what is the issue.
I download a xls file, but it doesent opens.
I had a mysql script like this, working, and I tried to convert it into mysqli and probably something is wrong...
Thanks in advance
$sqlExp = "SELECT * FROM table";
$countQryExp = mysqli_query($link, $sqlExp );
$filename = "sampledata.xls"; // File Name
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
while($row=mysqli_fetch_array($countQryExp,MYSQLI_ASSOC))
{
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
echo implode("\t", array_values($row)) . "\r\n";
}
There's a lot more to generating an Excel file than giving it a content type of application/vnd.ms-excel. Excel is a very particular format, whereas you're generating a TSV file - tab separated values, and in a pretty breakable manner (what happens if someone puts a \t in one of your site's fields, or a new line?).
If you want to generate real Excel files, you'll want one of the various libraries for doing so. If a CSV/TSV are fine, just export a .csv/.tsv file with proper headers.

Output 1,000s of records to text file

So I was hoping to be able to get by with a simple solution to read records from a database and save them to a text file that the user downloads. I have been doing this on the fly and for under 20,000 records, this works great. Over 20,000 records and I'm loading too much data into memory and PHP hits a fatal error.
My thought was to just grab everything in chunks. So I grab XX number of rows and echo them to the file and then loop to get the next XX rows until I'm done.
I am just echoing the results right now though, not building the file and then sending it for download, which I'm guessing I'll have to do.
The issue at this point succinctly is that with up to 20,000 rows, the file builds and downloads perfectly. With more than that, I get an empty file.
The code:
header('Content-type: application/txt');
header('Content-Disposition: attachment; filename="export.'.$file_type.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
// I do other things to check for records before, hence the do-while loop
$this->items = $model->getItems();
do {
foreach ($this->items as $k => $item) {
$i=0;
$tables = count($this->data['column']);
foreach ($this->data['column'] as $table => $fields) {
$columns = count($fields);
$j = 0;
foreach ($fields as $field => $junk) {
if ($quote_output) {
echo '"'.ucwords(str_replace(array('"'), array('\"'), $item->$field)).'"';
} else {
echo ''.$item->$field.'';
}
$j++;
if ($j<$columns) {
echo $delim;
}
}
$i++;
if ($i<$tables) {
echo $delim;
}
}
echo "\n";
}
} while($this->items = $this->_model->getItems());
Very large tables won't work that way.
You have to output the data as you read it from the database. If you need to sorted, then use the database ORDER BY for that purpose.
So more or less
// assuming you use a var such as $query to handle the DB
while(!$query->eof())
{
$fields = $query->read_next();
echo $fields; // with your formatting, maybe call a function...
}
The empty result is normal. If the memory is exhausted before any echo happens then nothing was sent to the browser.
Note also that PHP has a time limit (a watchdog) that you may need to tweak. The default is defined in your php.ini. You may set it to zero if you expect the tables to grow very much.
You should change your str_replace for addslashes(). This will probably free some memory.
Then I suggest you to save a file and use php file functions to do so: fopen() or file_put_contents().
I hope that might help you!
Actually, this might be simple fix. If PHP is running out of memory it's probably because the output buffer is filling before the file is sent. If so, simply flush() at regular intervals.
This will flush after each line:
do {
foreach(...) {
// assemble your output line here
}
echo "\n";
flush();
}
} while($this->items = $this->_model->getItems());
Flushing after each line might prove too slow, in which case add a counter and flush after every hundred, or whatever works best.

Giving images random name in PHP

this way i load image using php:
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("http://i.imgur.com/zWaQJNCb.jpg");
imagejpeg($image);
but problem is, if i want to save the image manually from my web browser to my desktop then all the images has same name like ix.jpeg [here file name is: ix.php] but i cant understand what is the way to configure the header so that images will have random name.. like 25xc.jpeg, 36s5a2f.jpeg... while saving it on desktop.. any idea?
this is will do the job!
$dt = date(time());
header("Content-type: image/jpeg");
header('Content-Disposition: inline; filename="'. $dt .'.jpg"');
$image=imagecreatefromjpeg("http://i.imgur.com/knNxDFnb.jpg");
imagejpeg($image);
Use the filename value in your header call. See Example 1.
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
I'll leave the "generate a random string" part up to you. I'd suggest basing it on the checksum of the file, but that's just me.
This is what you need to do if you would like to generate unique names for your users, all you need to do is to use actual time stamp and use the filename parameter in the header, you can do like below (if you use random generation there will be a very few cases where you will get the same name twice) :
$dt = date(time());
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="'. $dt .'.jpg"');
The above will generate unique images names like below :
1362504465.jpg
I hope this helps.
There are dozens if not hundred ways to do that.
Append time() after file name. This way all the file names will
have a (unique)number.
Use a hashing function, like md5().
Use this code-
function generateRandomName(len) {
$out = '';
for($i=0; $i<len; $i++) {
$out.=chr(rand(65,122));
}
return $out;
}
Simply append rand function after your file name (But then the file names won't be of equal length).
Google is your friend.
This would make an 8 characters long name of letters a-z, but you could do some magic and/or use http://www.asciitable.com/
function randChar() {
return char(rand(97, 122);
// Then you could either do a random number from 97 to 122 for a-z or just make an array of all the characters you would like in the filename.
}
$filename = '';
$amountOfChars = 8;
for($i = 0; $i < $amountOfChars; $i++; ) $filename .= randChar();
Hope this helps

Categories