如何在Excel中将图像从Excel工作表导入数据库

时间:2018-06-07 07:01:30

标签: php excel database

这是我用来将excel表中的数据插入db的代码。 但在这里我想将excel表中的图像插入db。 但如果我尝试包含图像的文件,它将转到代码的else部分。

是否有任何不同的功能可以将图像上传到db



<form action="" method="post" enctype="multipart/form-data">
	<br>
	<input type="file" name="file" id="file" accept=".xls,.xlsx">
	<button type="submit" id="submit" name="import" class="btn-submit">Import</button>
</form>
<?php

//excel sheet data insert
$conn = mysqli_connect("localhost","root","","hep");
require_once('C:\xampp\phpMyAdmin\vendor\php-excel-reader\excel_reader2.php');
require_once('C:\xampp\phpMyAdmin\vendor\SpreadsheetReader.php');

if (isset($_POST["import"])){
  $allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
  //echo $_FILES["file"]["type"];
  //exit;
  if(in_array($_FILES["file"]["type"],$allowedFileType)==false){
	  echo "only Excel Files Supported";
  } else {
    $targetPath = 'import/'.$_FILES['file']['name'];
    move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
    $Reader = new SpreadsheetReader($targetPath);
    $sheetCount = count($Reader->sheets());
		// print_r($sheetCount);
		//exit;
		for($i=0;$i<$sheetCount;$i++){
      $Reader->ChangeSheet($i);
      foreach ($Reader as $Row) {
        $name = "";
        if(isset($Row[0])) {
            $name = mysqli_real_escape_string($conn,$Row[0]);
        }
        $email = "";
        if(isset($Row[1])) {
            $email = mysqli_real_escape_string($conn,$Row[1]);
        }

        if (!empty($name) || !empty($description)) {
          $query = "insert into form(no,name) values('".$name."','".$email."')";
          $result = mysqli_query($conn, $query);
          if (! empty($result)) {
             echo "Excel Data Imported into the Database";
          } else {
              echo "Problem in Importing Excel Data";
          }
        }
			}
    }
  }
}
?>
&#13;
&#13;
&#13;

如果您有任何想法请发表您的答案.. 提前谢谢。

0 个答案:

没有答案
相关问题