ZipArchive::close(): Renaming temporary file failed: Invalid argument Phpexcel Laravel - phpexcel

I am working on append excel content into existing excel file and save it in a predefined directory with different name. I am using the Laravel 5.4 framework for my application.
I am using the below code to create excel file and save it on specified location:
$objPHPExcel = \PHPExcel_IOFactory::createReader('Excel2007');
//Load Existing excel file into I want to append
$objPHPExcel = $objPHPExcel->load(getDocumentPath().'/Report_Template_UG.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
//Here I have Written code to insert my data into loaded excel file
$today = date('d-m-Y');
$time = date('H:i:s');
$filename = 'Report_'.$today.' '.$time.'.xlsx';
//welcomed is the root directory name of laravel project
$dir = 'C:\xampp\htdocs\welcomed\storage\documents\';
$filepath = $dir . $filename;
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($path);
On executing the above code, I am gettign the below error:
ErrorException in Excel2007.php line 377:
ZipArchive::close(): Renaming temporary file failed: Invalid argument
Can anyone please tell me, What I have done wrong in above code or do I have missed something?
Thanks

Related

unable to load file from url phpoffice/spreadsheet

Trying to load an .xlsx file from url but it gives error like,
Fatal error: Uncaught InvalidArgumentException: File
"http://localhost/test/csvfile/samplesms.xlsx" does not exist. in
D:\wamp\www\test\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Shared\File.php on line 137
Loading file with below,
$filename = "http://localhost/test/csvfile/samplesms.xlsx";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load($filename);
File is already at given location.
you can load file content using php_get_content or curl and save it temporary locally then load local version of the file
$filename = "http://localhost/test/csvfile/samplesms.xlsx";
$file = file_get_contents($filename);
$inputFileName = 'tempfile.xlsx';
file_put_contents($inputFileName, $file);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
loading from URL is not supported in phpoffice/spreadsheet.

Excel 2010 cannot open certain size xls files created by PHPExcel

We have been using PHPExcel in our production environment for over 4 years now and it has been working great without any problem until last week when Excel 2010 & 2103 was not able to open one of many xls files generated by PHPExcel. The error which we got was
Error: "Excel found unreadable content in file. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes." .
We tried to repair and step through other suggestions we found but none worked.
After generating many files we were at least able to reproduce this issue consistently. Though it seems very strange but Excel 2010 & 2013 running on windows 7 & 10 is not able to open any files generated by phpExcel that are between 6914K - 6977K file size.
We cannot find any solution or reason why Excel 2010/2013 would not open these files. Are we missing something? Thanks in advance for any suggestions.
We are using...
PHPExcel: 1.8
Webserver: Apache/2.2.15 (Unix) & PHP 5.3.3 running on Red Hat
Sample code to reproduce the issue.
require_once dirname(dirname(__FILE__)) . '/Classes/PHPExcel.php';
$timestamp = date("YmdHis");
$filename = "report_" . $timestamp . ".xls";
$rowcount = 20150;
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
$objPHPExcel = new PHPExcel();
$VAR1 = '99999';
$VAR2 = 'XXX';
$VAR3 = 'XXXX';
$VAR4 = 'XXXXX';
$VAR5 = 'XXX';
$VAR6 = 'XXXXXXXXXXXXXXXXXXXX';
$VAR7 = 'XXX';
$VAR8 = 'XXXXXXXXXXXXXXXXXXX';
$VAR9 = 'XXXXXXXXXXXXXXXXXXX';
$VAR10 = '999999999';
$VAR11 = 'xxxxxxxxxxx';
$VAR12 = 'XXXXXXX';
$VAR13 = 'XXXX';
$VAR14 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$VAR15 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$VAR16 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$VAR17 = 'XXX';
$VAR18 = '99999';
$VAR19 = '9999999999';
$VAR20 = '999999999';
$VAR21 = 'XXXXXXXXXXXXXXXXXX';
$VAR22 = '99999';
$VAR23 = '999999';
for ( $i=2 ; $i<intval($rowcount) ; $i++ )
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A$i", "$VAR1")
->setCellValue("B$i", "$VAR2")
->setCellValue("C$i", "$VAR3")
->setCellValue("D$i", "$VAR4")
->setCellValue("E$i", "$VAR5")
->setCellValue("F$i", "$VAR6")
->setCellValue("G$i", "$VAR7")
->setCellValue("H$i", "$VAR8")
->setCellValue("I$i", "$VAR9")
->setCellValue("J$i", "$VAR10")
->setCellValue("K$i", "$VAR11")
->setCellValue("L$i", "$VAR12")
->setCellValue("M$i", "$VAR13")
->setCellValue("N$i", "$VAR14")
->setCellValue("O$i", "$VAR15")
->setCellValue("P$i", "$VAR16")
->setCellValue("Q$i", "$VAR17")
->setCellValue("R$i", "$VAR18")
->setCellValue("S$i", "$VAR19")
->setCellValue("T$i", "$VAR20")
->setCellValue("U$i", "$VAR21")
->setCellValue("V$i", "$VAR22")
->setCellValue("W$i", "$VAR23");
}
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("I")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("J")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("K")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("L")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("M")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("N")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("O")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("P")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("Q")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("R")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("S")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("T")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("U")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("V")->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("W")->setAutoSize(true);
ini_set("max_execution_time","900");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("reports/$filename");
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
unset($objWriter);
echo "<a href=reports/$filename>Download this file</a><br><br>";
Try to change this:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
To this:
$objWriter = new PHPExcel_Writer_Excel2007 ( $objPHPExcel );
And change the extension .xls to .xlsx

Wordpress : Genesis-Legacy Theme error

I am getting this error message on some of my pages within worrdpress dashboard.
Google Analytics Stats
Warning: array_merge(): Argument #1 is not an array in
/home/c5280den/public_html/wp-content/plugins/google-analyticator/google-api-php-client/src/Google_Client.php
on line 40
Fatal error: Class name must be a valid object or a string in
/home/c5280den/public_html/wp-content/plugins/google-analyticator/google-api-php-client/src/Google_Client.php
on line 104
Any help is most appreciated!!
Update your plugin bro.. If updated already, try this.
FILE - google-analyticator > google-api-php-client > src > Google_Client.php
OLD CODE (starting at Line 35)
require_once "config.php";
// If a local configuration file is found, merge it's values with the default configuration
if (file_exists(dirname(__FILE__) . '/local_config.php')) {
$defaultConfig = $apiConfig;
require_once (dirname(__FILE__) . '/local_config.php');
$apiConfig = array_merge($defaultConfig, $apiConfig);
NEW CODE (starting at Line 35)
require_once (dirname(__FILE__) . "/config.php");
// If a local configuration file is found, merge it's values with the default configuration
if (file_exists(dirname(__FILE__) . '/local_config.php')) {
$defaultConfig = $apiConfig;
require_once (dirname(__FILE__) . '/local_config.php');
$apiConfig = array_merge($defaultConfig, $apiConfig);
Adding to line 35...
require_once (dirname(__FILE__) . "/config.php");
in place of...
require_once "config.php";
seems to have fixed the problem.
Get it from here https://wordpress.org/support/topic/recent-update-throws-error-in-settings-page

Batch file to find a file, create a copy in the same location and run this on multiple directories

here's what I am trying to do.
I have a few hundred users My Documents folders in which most(not all) have a file(key.shk for shortkeys program).
I need to upgrade the software but doing so makes changes to the original file.
I would like to run a batch file on the server to find the files in each My Docs folder and make a copy of it there called backup.shk
I can then use this for roll back.
The folder structure looks like this
userA\mydocs
userB\mydocs
userC\mydocs
My tools are xcopy, robocopy or powershell
Thanks in advance
This powershell script works... save as .ps1
Function GET-SPLITFILENAME ($FullPathName) {
$PIECES=$FullPathName.split(“\”)
$NUMBEROFPIECES=$PIECES.Count
$FILENAME=$PIECES[$NumberOfPieces-1]
$DIRECTORYPATH=$FullPathName.Trim($FILENAME)
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($_.fullname)
$FILENAME = [System.IO.Path]::GetFileNameWithoutExtension($_.fullname)
return $FILENAME, $DIRECTORYPATH
}
$Directory = "\\PSFS03\MyDocs$\Abbojo\Insight Software"
Get-ChildItem $Directory -Recurse | where{$_.extension -eq ".txt"} | % {
$details = GET-SPLITFILENAME($_.fullname)
$name = $details[0]
$path = $details[1]
copy $_.fullname $path$name"_backup".txt
}

PHPExcel File format or extension is not valid

I'm using phpexcel for export my query in excel file; however after I created file(which is xslx format), I can not open my file in excel. It gives "the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the file format of the file" error. When I open the file in texteditor(mine is npp) I see my php file's css codes and some part of my html codes. My code is like that;
if( ! empty($_POST['export'])){
$query = "SELECT * FROM asd ORDER BY asdf LIMIT 10";
$headings = array('Timestamp', 'h1','h2');
require 'Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('List of Users');
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
$rowNumber = 2;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
$objPHPExcel->getActiveSheet()->freezePane('A2');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xlsx"');
header("Content-Transfer-Encoding: binary ");
//ob_end_clean();
//header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
I'm stucked please help. Thank you.
As described in the manual.... if anything else is being output to the browser, this will corrupt the output file.
Open the file in a text editor, and look for leading or trailing whitespace characters (spaces, tabs, newlines) or a BOM marker at the beginning of the output, or for any obvious PHP plaintext error messages in the content. These are the most obvious causes of this problem. Once you've identified the spurious characters, check through your script to see where that output is being generated, and remove it.
In your case, that means don't output your css and html.
EDIT
xlsx is the extension for an OfficeOpenXML Excel2007 file, not for a BIFF 8 xls file.... be consistent in your headers (mime type and file extension) and Writer
Either:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xls"');
or
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="userList.xlsx"');
Following documentation or comments somewhere I read, I had this:
$objWriter = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
// etc
instead of
$objWriter = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// etc
And that caused Firefox and IE/Edge to append an .xls to the filename and also give the error message when trying to open the file directly instead of downloading it. When downloading the file and opening the spreadsheet though, everything was fine.

Resources