What is error loading Excel file into Msql using PHP?

All kind time of day!

Fatal error: Uncaught PHPExcel_Reader_Exception: Could not open price_download_25.12.2011.xls for reading! File does not exist.?
Actually, we do not see the file/directory.
The file has been successfully saved in a folder, but PHPExcel doesn't want to work with him.

The task of loading data from excel file uploaded by the user using the form:

Form:
<form action="/function_upload.php" method="POST" enctype="multipart/form-data" >
 <label for="upload_file"></label>
 <input type="file" name="upload_file" accept="application/vnd.ms-excel"value="Excel"/>
 <input type="hidden" name="value1">
 br><br><input type="submit" value="Download Excel"/>
 </form>

PHP :
if ($_FILES['upload_file']['error'] > 0)
{
 echo '<p>Error:</p>';

 switch ($_FILES['upload_file']['error'])
{
 case 1: 
 echo 'file Size larger than allowed (upload_max_filesize in php.ini)';
break;
 case 2: 
 echo 'file Size larger than allowed (max_file_ize in the form)';
break;
 case 3: 
 echo 'Downloaded only part of the file';
break;
 case 4: 
 echo 'File was not uploaded';
break;
 case 6: 
 echo 'Download not possible: not specified temporary directory.';
break;
 case 7: 
 echo 'Download failed: could not write to server';
break;
}
exit();
}

if ($_FILES['upload_file']['type'] != 'application/vnd.ms-excel')
{
 echo 'the file is not excel file!';
exit();
}

// specify the place where you want to save the file
$upfile ='./uploads/' . $_FILES['upload_file']['name'];

// limit file size
$limit_size = 1*1024*1024; // 1 Mb
// valid file formats
$valid_format = array("xls");
// store errors
$error_array = array();
// path to the new file
$path_file = "./uploads/";

$file_excel = $_FILES['upload_file']['name'];
// file name without format
$file_excel_name = substr($file_excel, 0, -4);

if($_FILES){
 // validate file size
 if($_FILES["upload_file"]["size"] > $limit_size){
 $error_array[] = "file Size exceeds allowed!";
}
 // validate file format
 $format = end(explode(".", $_FILES["upload_file"]["name"]));
 if(!in_array($format, $valid_format)){
 $error_array[] = "file Format is not valid!";
}
 // if no error occurred
if(empty($error_array)){
 // check whether the uploaded file
if(is_uploaded_file($_FILES["upload_file"]["tmp_name"])){
 // save the file
 move_uploaded_file($_FILES["upload_file"]["tmp_name"], $path_file . $file_excel_name . ".$format" );
// fopen($file_excel_name, r);
}else{
 // If the file is not loaded
 $error_array[] = "upload Error!";
}
 } 
}

$connection = new mysqli("localhost", "root", "", "eshop");
$connection->set_charset("utf8");

require_once './PHPExcel/Classes/PHPExcel.php';
require_once './PHPExcel/Classes/PHPExcel/IOFactory.php';

$PHPExcel_file = PHPExcel_IOFactory::load($file_excel_name);

foreach ($PHPExcel_file->getWorksheetIterator() as $worksheet) {
 $columns_str = "";
 $columns_count = PHPExcel_Cell::columnIndexFromString($worksheet->getHighestColumn());

 for ($column = 0; $column < $columns_count; $column++) {
 $columns_str .= ($columns_name_on1line ? "column" . $column : $worksheet->getCellByColumnAndRow($column, 1)->getCalculatedValue()) . ",";
}

 $columns_str = substr($columns_str, 0, -1);

 $connection->query("DROP TABLE IF EXISTS exceltable");
 $connection->query("CREATE TABLE exceltable (" . str_replace(",", "TEXT NOT NULL", $columns_str) . "TEXT NOT NULL)");

 $rows_count = $worksheet->getHighestRow();

 for ($row = 1; $row <= $rows_count; $row++) {
 $value_str = "";

 for ($column = 0; $column < $columns_count; $column++) {
 $merged_value = "";
 $cell = $worksheet->getCellByColumnAndRow($column, $row);

 foreach ($worksheet->getMergeCells() as $mergedCells) {
 if ($cell->isInRange($mergedCells)) {
 $merged_value = $worksheet->getCell(explode(":", $mergedCells)[0])->getCalculatedValue();
break;
}
}

 $value_str .= "'" . (strlen($merged_value) == 0 ? $cell->getCalculatedValue() : $merged_value) . "',";
}

 $value_str = substr($value_str, 0, -1);

 $connection->query("INSERT INTO exceltable (" . $columns_str . ") VALUES (" . $value_str . ")");
}
}
April 4th 20 at 00:33
1 answer
April 4th 20 at 00:35
Solution
You have $file_excel_name is the name of the file without path and extension
I've been looking for the answer, did a shitload of other stuff, but it was all so simple...
Thank you:)) - Elisa65 commented on April 4th 20 at 00:38

Find more questions by tags PHP