Read two excl files and Pictures folder and move the picture files to other folders excl Content upload Database

Source: Internet
Author: User
$name =$_post;
Include ('.. /phpexcel.php ');
Include '. /phpexcel/writer/excel2007.php ';
Require '. /.. /.. /data/config.php ';
Define (' File_url ', ' ... /.. /.. /attachment/');//Path to save
Define ("FILE1", "... /.. /.. /ftp_file/file1.xlsx ");//file 1 Detailed Product info
Define ("FILE2", "... /.. /.. /ftp_file/file1.xlsx ");//File 2 product Source Address
Define ("File_img", "... /.. /.. /ftp_file/contentpic/. "); /contentpic Pictures folder
$number =$_post[' number '];//get people
$setime =$_post[' setime '];//get time
Print_r ($_files);//Get file path, etc.
Move_uploaded_file ($_files[' file_1 ' [' tmp_name '], ' file1.xlsx ');
---------------------------------------------------------------------------------------determine if the user uploaded the FILE1 and FILE2 files
if (File_exists (". /.. /.. /ftp_file/file1.xlsx ") &&file_exists (".. /.. /.. /ftp_file/file2.xlsx ")) {
}else{
echo "Sorry file does not exist, please upload file";
Exit
}
----------------------------------------------------------------------------------------FILE1 file gets
Cycle
$filePath = ". /.. /.. /ftp_file/file1.xlsx ";
Create a 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 where you can read the file from an Excel object, or write it to a file
$PHPExcel = $PHPReader->load ($filePath);
/** reading the first worksheet in an Excel file */
$currentSheet = $PHPExcel->getsheet (0);
/** get the largest column number */
$allColumn = $currentSheet->gethighestcolumn ();
How many lines does the/** get altogether? */
$allRow = $currentSheet->gethighestrow ();
Iterates through the contents of each cell. Note that the row starts at 1 and the column starts 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 ',
); Specifies the array field name that corresponds 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 ();
if ($cell instanceof Phpexcel_richtext)//Rich Text conversion string
$cell = $cell->__tostring ();
$arr [$temp [$colIndex]]= $cell;
}
$CD _array[] = $arr;
Unset ($arr);
}
---------------------------------------------------------------------------------------------------FILE2 file gets
$filePath = ". /.. /.. /ftp_file/file2.xlsx ";
Create a Reader object
$PHPReader = new phpexcel_reader_excel2007 ();
if (! $PHPReader->canread ($filePath)) {
$PHPReader = new Phpexcel_reader_excel5 ();
if (! $PHPReader->canread ($filePath)) {
Echo ' no Excel ';
return;
}
}
Create an Excel object where you can read the file from an Excel object, or write it to a file
$PHPExcel = $PHPReader->load ($filePath);
/** reading the first worksheet in an Excel file */
$currentSheet = $PHPExcel->getsheet (0);
/** get the largest column number */
$allColumn = $currentSheet->gethighestcolumn ();
How many lines does the/** get altogether? */
$allRow = $currentSheet->gethighestrow ();
Iterates through the contents of each cell. Note that the row starts at 1 and the column starts from a
Create a 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 ();
if ($cell instanceof Phpexcel_richtext)//Rich Text conversion string
$cell = $cell->__tostring ();
$arr [] = $cell;
}
$url _array[] = $arr;
Unset ($arr);
}
foreach ($cd _array as $key = + $value) {
foreach ($url _array as $key 1 = $value 1) {
if ($value [' name ']== $value 1[0]) {

$CD _array[$key] [' url '] = $value 1[1];
}
}
}
$CD _info=array ();
$CD _info1=array ();
foreach ($cd _array as $key = + $value) {
foreach ($value as $key 1 = $value 1) {
if ($key 1== ' number ' | | $key 1== ' class ' | | $key 1== ' Macth ' | | $key 1== ' descript ' | | $key 1== ' url ' | | $key 1== ' name ') {
$CD _info[$key 1] = $value 1;
}
}
$CD _info1[]= $CD _info;
}
------------------------------------------------------------------------------------------------------loop all picture paths to an array
$j =0;//variables
$arr = Array ();//arrays
$CD _img=array ();
foreach ($cd _info1 as $key = + $value) {

foreach ($value as $key 1=> $value 1) {
if ($key 1== ' descript ') {
$text = $value 1;//Assignment
Preg_match_all ("/]*) \s*src= (' |\") ([^ ' \ "]+) (' |\ ')/", $text, $matches);
$new _arr=array_unique ($matches [0]);
$arr = Array ();
foreach ($new _arr as $val)
{
$i = Explode (' src= ', $val) [1];
$arr [] = substr ($i, 1,strlen ($i)-2);
}
}
}
$CD _img[]= $arr;
}
-----------------------------------------function Encapsulation--------------------------------------1---------
function Hover_file ($file _url, $file _name) {
Determine and create a folder
$y =date (' Y ', Time ());//year
$m =date (' m ', Time ());
if (!is_dir ('.. /.. /.. /attachment/images/'. $y. $m)) {
MkDir ('.. /.. /.. /attachment/images/'. $y. $m, 0777);//Create Year + Month folder permissions are highest
}
Get the picture path and move
$text = $file _url;//picture Path 1
$file _url;
$file _url;
$text 1= $file _name;//picture Path 2
$text 1;
Rename (Iconv ("UTF-8", "GBK", $text), Iconv ("UTF-8", "GBK", $text 1));
}
------------------------------------------------------------------------------------------------Add all the data to the database
New data
$uid =array ();
$k _n=0;
foreach ($cd _info1 as $key = + $value) {
Var_dump ($value);
Data additions
$img _list= $cd _img[$k _n];
A page that loops through the storage details
$img _list1=null;
foreach ($img _list as $key 3 = $value 3) {
$img _list1= $img _list1. "

";
}
------------------------------------------Add a main diagram--------------------------------------------------
$lotimg =0;//
foreach ($cd _img[$k _n] as $key 1 = $value 1) {
$arr _1=explode ('/', $value 1);
$number 1= $arr _1[count ($arr _1)-1];
$number 2= $arr _1[count ($arr _1)-2];
$number = ". /.. /.. /ftp_file/contentpic/". $number 2. ' /'. $number 1;//Get the picture True path
Determine if the height of the picture is normal
$aa =getimagesize ($number);
$width = $aa [' 0 '];
$as = getimagesize (Iconv ("UTF-8", "GBK", $number));
$height = $as ["1"];///get pictures of high
Echo $height;
if ($height >220) {
$imgs =basename ($number);
Break
}else{
$imgs = ";
}
$lotimg =lotimg+1;
if ($lotimg =>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 '],//buy price
' Oprice ' = $value [' macth ']*0.2+ $value [' macth '],//purchase price separately
' Mprice ' = $value [' macth ']*0.5+ $value [' macth '],//original price
' gimg ' = ' images/'. Date (' Y ', Time ()). Date (' m ', Time ()). ' /'. $imgs,//main chart
' Groupnum ' = rand (2, 24),//Group size
' Groupnum ' = $tuan _number,//number of people in the group
' Salenum ' =>rand (7, 999),//Sold Quantity
' Endtime ' =>rand (8, 24),//Group time limit
' Endtime ' = $tuan _data,
' Gdetaile ' = $img _list1,
' Gnum ' = $value [' Number '],//stock
' Gdesc ' + $img _list1,//Detail Page scrolling pictures
);
Pdo_insert (' Tg_goods ', $data);
$set = Pdo_fetch ("SELECT * from". TableName (' Tg_goods '). "Where Gname= '". $value [' name ']. "'");
$uid []= $set [' ID '];//get ID
New picture Path
}else{
echo "Sorry, file already exists.";

}
$k _n= $k _n+1;

}
--------------------------------------------------------------------------------------------picture into the database
Var_dump ($uid [0]);
if ($uid [0]!= ') {
$j _n=0;//self-increment variable
foreach ($cd _img as $key = + $value) {
'
';
foreach ($value as $key 2 = $value 2) {
$y =date (' Y ', Time ()), $m =date (' m ', Time ());//The folder name takes years
$img _url= "images/". $y. $m. '/'. basename ($value 2);
$data = Array (
' g_id ' = $uid [$j _n],
' Thumb ' = $img _url,
);
Echo $data;
Pdo_insert (' Tg_goods_atlas ', $data);
$uid [$j _n]. ' --'. $img _url. '--'. '
';
}
$j _n= $j _n+1;
}
echo ' picture storage succeeded ';
}
$set = Pdo_fetch ("SELECT * from". TableName (' Tg_goods '). "Where fk_typeid=". $value [' class ']. "");
$set [' Gname ']. '
';
----------------------------------------------$CD _img is the path to the image array to get to-perform a looping move of the picture----------------------------
foreach ($cd _img as $key = + $value) {
foreach ($value as $key 1 = $value 1) {
$arr _1=explode ('/', $value 1);
Print_r ($arr _1);
$number 1= $arr _1[count ($arr _1)-1];
$number 2= $arr _1[count ($arr _1)-2];

$url _name= $number 2. ' /'. $number 1;//Relative folder path/xxxxxx/xxx.jpg
$img _name=basename ($value 1);//Picture file name
$hostdir =dirname (". /.. /.. /ftp_file/contentpic/. "); /Set Picture path
Gets the file and folder name in the Scan folder to be stored in an array $filesnames
$filesnames = Scandir ($hostdir); File name Directory
$y =date (' Y ', Time ());//year
$m =date (' m ', Time ());
$text = $hostdir. ' /'. $url _name; Picture Path 1
$text 1 = "... /.. /.. /attachment/images/". $y. $m." /". $img _name."; /Picture Path 2
Hover_file ($text, $text 1);//execute 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 {
Deldir ($fullpath);
}
}
}
Closedir ($DH);
Delete the current folder:
if (RmDir ($dir)) {
return true;
} else {
return false;
}
}
Deldir ('.. /.. /.. /ftp_file/contentpic ');//Delete folder directory
$file 1 = "... /.. /.. /ftp_file/file1.xlsx ";
$file 2 = "... /.. /.. /ftp_file/file2.xlsx ";
Unlink ($file 1);//delete file 1
Unlink ($file 2);//delete file 2
echo "";
?>

The above describes the reading of two excl files and Pictures folder and moving the picture files in other folders excl content upload database, including the contents of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.