Home > Backend Development > PHP Tutorial > Read two excl files and picture folders and move the picture files to other folders and upload the excl content to the database

Read two excl files and picture folders and move the picture files to other folders and upload the excl content to the database

WBOY
Release: 2016-07-28 08:27:05
Original
56278 people have browsed it

$name=$_POST;
include ('../PHPExcel.php');
include '../PHPExcel/Writer/Excel2007.php';
require '../../.. /data/config.php';
define('FILE_URL','../../../attachment/');//Saved path
define("FILE1","../../. ./FTP_FILE/file1.xlsx");//File 1 Detailed product information
define("FILE2","../../../FTP_FILE/file1.xlsx");//File 2 Product source address
define("FILE_IMG","../../../FTP_FILE/contentPic/..");//contentPic picture folder
// $number=$_POST['number'];//Get the number of people
// $setime=$_POST['setime']; // Get the time
// print_r($_FILES); // Get the file path, etc.
// move_uploaded_file($_FILES['file_1']['tmp_name'], 'FILE1.xlsx');
//---------------------------------------- --------------------------------------------------Determine whether the user Uploaded FILE1 and FILE2 files
if(file_exists("../../../FTP_FILE/FILE1.xlsx")&&file_exists("../../../FTP_FILE/FILE2.xlsx")){
}else{
echo "Sorry the file does not exist, please upload the file";
exit;
}
//-------------------------- -------------------------------------------------- ------------FILE1 file acquisition
//Loop
$filePath = "../../../FTP_FILE/FILE1.xlsx";
//Create reader object
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
 'No Excel ';
Return;
}}}
// Create an excel object, at this time you can read the file through the excel object, or you can write it through it
$ phpexcel = $ phpreader- & gt; load ($ filepath);
/**Read the first worksheet in excel file*/
$currentSheet = $PHPExcel->getSheet(0);
/**Get the largest column number*/
$allColumn = $currentSheet->getHighestColumn();
/**Get the total number of rows*/
$allRow = $currentSheet->getHighestRow();
//Loop to read the contents of each cell.Note that rows start from 1 and columns start from A
$cd_array=array();
$arr =array();
$temp=array(
'A'=>'name',
'B'=> 'class',
'C'=>'C',
'D'=>'D',
'E' =>'E',
'F' =>'F',
' G'=>'G',
'H'=>'macth',
'I'=>'I',
'J'=>'number',
'K'=>' K',
'L'=>'L',
'M'=>'M',
'N'=>'N',
'O'=>'O',
'P '=>'P',
'Q'=>'Q',
'R'=>'R',
'S'=>'S',
'T'=>'T ',
'U'=>'descript',
'V'=>'V',
'W'=>'W',
'X'=>'X',
'Y' =>'Y',
); //Specify the array field name corresponding to the generated excel table
for($rowIndex=4;$rowIndex<=$allRow;$rowIndex++){
for($colIndex='A'; $colIndex<='Y';$colIndex++){
            $addr = $colIndex.$rowIndex;                       $cell = $currentSheet->getCell($addr)->getValue(); ) //Rich text conversion string
            $cell = $cell->__toString();                     $arr[$temp[$colIndex]]=$cell; $arr);
}
//---------------------------------------- -------------------------------------------------- --------FILE2 file acquisition
$filePath = "../../../FTP_FILE/FILE2.xlsx";
//Create reader object
$PHPReader = new PHPExcel_Reader_Excel2007();
if( !$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
          echo 'no Excel';
}
}
//Create an excel object. At this time, you can either read files through the excel object or write files through it
$PHPExcel = $PHPReader->load($filePath);
/**Read the first worksheet in excel file */
$currentSheet = $PHPExcel->getSheet(0);
/**Get the largest column number*/
$allColumn = $currentSheet->getHighestColumn();
/**Get the total number of rows*/
$allRow = $currentSheet->getHighestRow();
//Loop to read the contents of each cell.Note that rows start from 1 and columns start from A
//Create reader object
$url_array=array();
$arr =array();
for($rowIndex=2;$rowIndex<=$allRow;$rowIndex++) {
for($colIndex='A';$colIndex<='B';$colIndex++){
$addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)-> getValue();
                                                                                                                                        $url_array[] = $ arr;
unset($arr);
} foreach ($cd_array as $key => $value) {
foreach ($url_array as $key1 => $value1) {
if($value['name' ]==$value1[0]){
                                                                    }
          }
$cd_info=array();
$cd_info1=array( );
foreach ($cd_array as $key => $value) {
foreach ($value as $key1 => $value1) {
foreach if($key1=='number'||$key1=='class '||$key1=='macth'||$key1=='descript'||$key1=='url'||$key1=='name'){
                                      $cd_info[$key1] = $value1;
                                                                                                                                                        -------------------------------------------------- --------------------Loop all image paths into arrays
$j=0;//Variable
$arr = array();//Array
$cd_img= array();
foreach($cd_info1 as $key=>$value){

                          $text = $ value1; // Assignment
preg_match_all ("/& lt; img ([^& gt;]*) s*src = ('|") ([^' "]/('|"), $ text . foreach($new_arr as $val)
                                                                ) [1];        }
    $cd_img[]=$arr;
    }
// --- ----------------------------------Function encapsulation---------- ----------------------------1---------
function hover_file($file_url,$file_name){
//Judge and create the folder
$y=date('Y',time());//Year
$m=date('m',time());
if(!is_dir('../ ../../attachment/images/'.$y.$m)){
mkdir('../../../attachment/images/'.$y.$m,0777);// The permissions of the folder created in the year + month are the highest
}
//Get the image path and move it
$text=$file_url;//Picture path 1
// $file_url;
// $file_url;
$text1=$file_name;//Picture path 2
$text1;
rename (iconv("UTF-8","GBK",$text),iconv("UTF-8","GBK",$text1));
}
//---------- -------------------------------------------------- ----------------------------------Add all data to the database
              //Add new data
                $ uid=array ();                                                                                                                                                              . k_n];
                                                                                                                                                                                                                                                                                                                                                                           $img_list1=$img_list1."

";
                                                                                                 -Add main image------------------------------------------------- ----
                                                                                                                                                                                            $arr_1=explode('/',$value1);
$arr_1=explode('/',$value1);
$number1 =$arr_1[count($arr_1)-1];
                                                                                                                                  ​FILE/contentPic/".$ number2.'/'.$number1;//Get the real path of the image
                                                                                                                                                                                                                      // $width=$aa['0'];
                                    $as= getimagesize(iconv("UTF-8","GBK",$number));                                                                       // echo $height;
if($height>220){
                                                                                                                                    $lotimg=lotimg+1;
                                                                                                                                                         5){break;}
              }
                    $a = "select * from " . tablename('tg_goods') . "where gsn=".$value['class']."";
                    $set = pdo_fetch("select * from " . tablename('tg_goods') . "where gname='".$value['name']."'");
                    if($set['gname']!=$value['name']){
                         $data = array(
                            'gname' =>$value['name'],
                            'fk_typeid' =>$tuan_class,
                            'category_parentid'=>$tuan_class,
                            'gsn'=>$value['class'],
                            'gprice'=>$value['maprric'],
                            'uniacid'=>'1',
                            'isshow'=>'1',
                            'createtime'=>time(),
                            'gprice'=>$value['macth'],//团购价格
                            'oprice'=>$value['macth']*0.2+$value['macth'],//单独购买价格
                            'mprice'=>$value['macth']*0.5+$value['macth'],//原价
                            'gimg'=>'images/'.date('Y',time()).date('m',time()).'/'.$imgs,   //主图
                            // 'groupnum'=> rand(2, 24),//拼团人数
                             'groupnum'=>$tuan_number,//拼团人数
                            'salenum'=>rand(7, 999),//已出售数量
                            // 'endtime'=>rand(8, 24),//拼团时间限制
                            'endtime'=>$tuan_data,
                            'gdetaile'=>$img_list1,
                            'gnum'=>$value['number'],//库存
                            // 'gdesc'=>$img_list1,//详情页滚动图片
                            );
                            pdo_insert('tg_goods', $data);
                            $set = pdo_fetch("select * from " . tablename('tg_goods') . "where gname='".$value['name']."'");
                            $uid[]=$set['id'];//获取id
                          //图片路径新增
                            }else{
                             echo  "sorry,文件已存在.";
                           
                            }
                            $k_n=$k_n+1;
                    
                }
                  //-------------------------------------------------- ------------------------------------------------ Pictures stored in database
var_dump($uid[0]); ) {
'& lt;/br & gt;';
Foreach ($ value as $ key2 = & gt; $ value2) {
$ y = date ('y', time ()); );//The year and month required for the folder name
                                                                                                                                                                         uid[$j_n],
                                                                                                                                                                               echo $data; // $uid[$j_n].'--'. $ img_url .'- '.' & lt;/br & gt; ';}}
$ j_n = $ j_n+1; from " . tablename('tg_goods') . "where fk_typeid=".$value['class']."");
                                                                                                                                                             ​--------------------------------------------------$cd_img is obtained The path to the image array - execute a loop to move the image----------------------------------
foreach ($cd_img as $key => $ value) {
foreach($value as $key1 =>$value1){
$arr_1=explode('/',$value1);
// print_r($arr_1);
$number1=$arr_1[count( $arr_1)-1];
$number2=$arr_1[count($arr_1)-2];

$url_name=$number2.'/'.$number1;//relative folder path/xxxxxx/xxx.jpg
$img_name=basename($value1);//Picture file name
$hostdir=dirname("../../../FTP_FILE/contentPic/..");//Set the picture path
//Get it too It is to scan the files and folder names in the folder and store them in the array $filesnames
$filesnames = scandir($hostdir); //File name directory
$y=date('Y',time());//Year
$m=date('m',time());
$text= $hostdir.'/'.$url_name; //Image path 1
$text1="../../../attachment/images /".$y.$m."/".$img_name."";//Picture path 2
hover_file($text,$text1);//Execute the move file function
}
$arr_1='';
}
//------------------------------------------------ ---------------Delete all files
function deldir($dir) {
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath );
    } else {
                                                                                           
closedir($dh);
//Delete the current folder:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
deldir('../.. /../FTP_FILE/contentPic');//Delete the folder directory
$file1 = "../../../FTP_FILE/FILE1.xlsx";
$file2 = "../../.. /FTP_FILE/FILE2.xlsx";
unlink($file1);//Delete file 1
unlink($file2);//Delete file 2
echo "<script>alert('The product is imported successfully, click OK to jump '); window.location.href=document.referrer;</script>";
?>

The above has introduced how to read two excl files and picture folders, move the picture files to other folders, and upload the excl content to the database, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template