So, I’m trying to upload xls and xlsx files to my server (NginX on a Yii framework), which has a php script to parse the data and add it to the database.
Simple enough.
However, I’m stumbling at the first block; when uploading the file, PHPExcel can’t seem to find the file -and to be honest, I’m not sure it’s there, because I can’t get a solid understanding of where it should be. Below is my code
<code>
<?php
spl_autoload_unregister(array('YiiBase','autoload'));
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/share/nginx/www/dashboard/protected/extensions/phpexcel/Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
spl_autoload_register(array('YiiBase','autoload'));
echo $_FILES . "<br />";
$inputFileName = $_POST;
foreach ($inputFileName as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
echo $inputFileName["file"] . "<br />";
echo 'Loading file ',pathinfo($inputFileName["file"],PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
//This is where it breaks
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName["file"]);
</code>
And when it breaks, it hands me this error:
<code>
Could not open Active, Telemetered, Electricity Accounts 17-10-2012.xlsx for reading! File does not exist.
</code>
and this stack trace:
<code>
/**
225 * Can the current PHPExcel_Reader_IReader read the file?
226 *
227 * @param string $pFileName
228 * @return boolean
229 * @throws Exception
230 */
231 public function canRead($pFilename)
232 {
233 // Check if file exists
234 if (!file_exists($pFilename)) {
235 throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
236 }
237
238 // Check if zip class exists
239 if (!class_exists('ZipArchive',FALSE)) {
240 throw new Exception("ZipArchive library is not enabled");
241 }
</code>
Now, as I understand it, pathinfo() should have given PHP what it needs to find the file, and that file should be living in php’s temp folder once I hit the upload button (simple html form; I can get the filename from $_POST, so I know it gets that far), but either it’s not actually uploading or it’s getting lost once it’s up.
Any help is much appreciated!
EDIT: html upload form:
<code>
<form action="testFile" method="post">
<label for="file" />
<input type="file" name="file" />
<br />
<input type="submit">
</form>
</code>
EDIT:
The html upload form was at fault; since I didn’t set the enctype, $_FILES didn’t get populated. Setting the encoding to multipart/form data let me get at the information I needed. Thank you both!
Since you are using Yii use the
CUploadedFileclass, callgetInstanceto get access and information regarding the uploaded files. According to Yii Documentation on CUploadedFileFor example: