读取两个excl文件和图片文件夹 并将其中的图片文件移动其他文件夹excl内容上传数据库
程序员文章站
2022-03-29 13:57:32
...
$name=$_POST;
include ('../PHPExcel.php');
include '../PHPExcel/Writer/Excel2007.php';
require '../../../data/config.php';
define('FILE_URL','../../../attachment/');//保存的路径
define("FILE1","../../../FTP_FILE/file1.xlsx");//文件1 详细商品信息
define("FILE2","../../../FTP_FILE/file1.xlsx");//文件2 商品来源地址
define("FILE_IMG","../../../FTP_FILE/contentPic/..");//contentPic图片文件夹
// $number=$_POST['number'];//获取人数
// $setime=$_POST['setime'];//获取时间
// print_r($_FILES);//获取文件路径等
// move_uploaded_file($_FILES['file_1']['tmp_name'], 'FILE1.xlsx');
//---------------------------------------------------------------------------------------判断用户是否上传了FILE1和FILE2文件
if(file_exists("../../../FTP_FILE/FILE1.xlsx")&&file_exists("../../../FTP_FILE/FILE2.xlsx")){
}else{
echo "对不起文件不存在,请上传文件";
exit;
}
//----------------------------------------------------------------------------------------FILE1文件获取
//循环
$filePath = "../../../FTP_FILE/FILE1.xlsx";
//建立reader对象
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
'no Excel';
return ;
}
}
//建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
//循环读取每个单元格的内容。注意行从1开始,列从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',
); //指定生成excel表对应的数组字段名
for($rowIndex=4;$rowIndex for($colIndex='A';$colIndex $addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText) //富文本转换字符串
$cell = $cell->__toString();
$arr[$temp[$colIndex]]=$cell;
}
$cd_array[] = $arr;
unset($arr);
}
//---------------------------------------------------------------------------------------------------FILE2文件获取
$filePath = "../../../FTP_FILE/FILE2.xlsx";
//建立reader对象
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
echo 'no Excel';
return ;
}
}
//建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
//循环读取每个单元格的内容。注意行从1开始,列从A开始
//建立reader对象
$url_array=array();
$arr =array();
for($rowIndex=2;$rowIndex for($colIndex='A';$colIndex $addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText) //富文本转换字符串
$cell = $cell->__toString();
$arr[] = $cell;
}
$url_array[] = $arr;
unset($arr);
}
foreach ($cd_array as $key => $value) {
foreach ($url_array as $key1 => $value1) {
if($value['name']==$value1[0]){
$cd_array[$key]['url'] = $value1[1];
}
}
}
$cd_info=array();
$cd_info1=array();
foreach ($cd_array as $key => $value) {
foreach ($value as $key1 => $value1) {
if($key1=='number'||$key1=='class'||$key1=='macth'||$key1=='descript'||$key1=='url'||$key1=='name'){
$cd_info[$key1] = $value1;
}
}
$cd_info1[]=$cd_info;
}
//------------------------------------------------------------------------------------------------------循环所有图片路径为数组
$j=0;//变量
$arr = array();//数组
$cd_img=array();
foreach($cd_info1 as $key=>$value){
foreach($value as $key1=>$value1){
if($key1=='descript'){
$text=$value1;//赋值
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;
}
// -----------------------------------------函数封装--------------------------------------1---------
function hover_file($file_url,$file_name){
//判断并创建文件夹
$y=date('Y',time());//年
$m=date('m',time());
if(!is_dir('../../../attachment/images/'.$y.$m)){
mkdir('../../../attachment/images/'.$y.$m,0777);//创建年+月的文件夹权限为最高
}
//获取图片路径并移动
$text=$file_url;//图片路径1
// $file_url;
// $file_url;
$text1=$file_name;//图片路径2
$text1;
rename(iconv("UTF-8","GBK",$text),iconv("UTF-8","GBK",$text1));
}
//------------------------------------------------------------------------------------------------把所有的数据添加到数据库
//新增数据
$uid=array();
$k_n=0;
foreach ($cd_info1 as $key => $value) {
// var_dump($value);
// 数据新增
$img_list= $cd_img[$k_n];
//循环存储详细信息的页面
$img_list1=null;
foreach ($img_list as $key3 => $value3) {
$img_list1=$img_list1."";
}
//------------------------------------------添加主图--------------------------------------------------
$lotimg=0;//定数器
foreach($cd_img[$k_n] as $key1 =>$value1){
$arr_1=explode('/',$value1);
$number1=$arr_1[count($arr_1)-1];
$number2=$arr_1[count($arr_1)-2];
$number = "../../../FTP_FILE/contentPic/".$number2.'/'.$number1;//获取图片真实路径
//判断图片的高度是否正常
// $aa=getimagesize($number);
// $width=$aa['0'];
$as= getimagesize(iconv("UTF-8","GBK",$number));
$height=$as["1"];///获取图片的高
// 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'],//团购价格
'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;
}
//--------------------------------------------------------------------------------------------图片存入数据库
var_dump($uid[0]);
if($uid[0]!=''){
$j_n=0;//自增变量
foreach ($cd_img as $key => $value) {
'';
foreach ($value as $key2 => $value2) {
$y=date('Y',time());$m=date('m',time());//文件夹名字需要的年月
$img_url="images/".$y.$m.'/'.basename($value2);
$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 '图片储存成功';
}
// $set = pdo_fetch("select * from " . tablename('tg_goods') . "where fk_typeid=".$value['class']."");
// $set['gname'].'';
//----------------------------------------------$cd_img是获取到的图片数组路径-执行循环移动图片----------------------------
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;//相对文件夹路径/xxxxxx/xxx.jpg
$img_name=basename($value1);//图片文件名字
$hostdir=dirname("../../../FTP_FILE/contentPic/..");//设置图片路径
//获取也就是扫描文件夹内的文件及文件夹名存入数组 $filesnames
$filesnames = scandir($hostdir); //文件名称目录
$y=date('Y',time());//年
$m=date('m',time());
$text= $hostdir.'/'.$url_name; //图片路径1
$text1="../../../attachment/images/".$y.$m."/".$img_name."";//图片路径2
hover_file($text,$text1);//执行移动文件函数
}
$arr_1='';
}
//-------------------------------------------------------------删除所有文件
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);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
deldir('../../../FTP_FILE/contentPic');//删除文件夹目录
$file1 = "../../../FTP_FILE/FILE1.xlsx";
$file2 = "../../../FTP_FILE/FILE2.xlsx";
unlink($file1);//删除文件1
unlink($file2);//删除文件2
echo "";
?>
include ('../PHPExcel.php');
include '../PHPExcel/Writer/Excel2007.php';
require '../../../data/config.php';
define('FILE_URL','../../../attachment/');//保存的路径
define("FILE1","../../../FTP_FILE/file1.xlsx");//文件1 详细商品信息
define("FILE2","../../../FTP_FILE/file1.xlsx");//文件2 商品来源地址
define("FILE_IMG","../../../FTP_FILE/contentPic/..");//contentPic图片文件夹
// $number=$_POST['number'];//获取人数
// $setime=$_POST['setime'];//获取时间
// print_r($_FILES);//获取文件路径等
// move_uploaded_file($_FILES['file_1']['tmp_name'], 'FILE1.xlsx');
//---------------------------------------------------------------------------------------判断用户是否上传了FILE1和FILE2文件
if(file_exists("../../../FTP_FILE/FILE1.xlsx")&&file_exists("../../../FTP_FILE/FILE2.xlsx")){
}else{
echo "对不起文件不存在,请上传文件";
exit;
}
//----------------------------------------------------------------------------------------FILE1文件获取
//循环
$filePath = "../../../FTP_FILE/FILE1.xlsx";
//建立reader对象
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
'no Excel';
return ;
}
}
//建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
//循环读取每个单元格的内容。注意行从1开始,列从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',
); //指定生成excel表对应的数组字段名
for($rowIndex=4;$rowIndex for($colIndex='A';$colIndex $addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText) //富文本转换字符串
$cell = $cell->__toString();
$arr[$temp[$colIndex]]=$cell;
}
$cd_array[] = $arr;
unset($arr);
}
//---------------------------------------------------------------------------------------------------FILE2文件获取
$filePath = "../../../FTP_FILE/FILE2.xlsx";
//建立reader对象
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
echo 'no Excel';
return ;
}
}
//建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
//循环读取每个单元格的内容。注意行从1开始,列从A开始
//建立reader对象
$url_array=array();
$arr =array();
for($rowIndex=2;$rowIndex for($colIndex='A';$colIndex $addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText) //富文本转换字符串
$cell = $cell->__toString();
$arr[] = $cell;
}
$url_array[] = $arr;
unset($arr);
}
foreach ($cd_array as $key => $value) {
foreach ($url_array as $key1 => $value1) {
if($value['name']==$value1[0]){
$cd_array[$key]['url'] = $value1[1];
}
}
}
$cd_info=array();
$cd_info1=array();
foreach ($cd_array as $key => $value) {
foreach ($value as $key1 => $value1) {
if($key1=='number'||$key1=='class'||$key1=='macth'||$key1=='descript'||$key1=='url'||$key1=='name'){
$cd_info[$key1] = $value1;
}
}
$cd_info1[]=$cd_info;
}
//------------------------------------------------------------------------------------------------------循环所有图片路径为数组
$j=0;//变量
$arr = array();//数组
$cd_img=array();
foreach($cd_info1 as $key=>$value){
foreach($value as $key1=>$value1){
if($key1=='descript'){
$text=$value1;//赋值
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;
}
// -----------------------------------------函数封装--------------------------------------1---------
function hover_file($file_url,$file_name){
//判断并创建文件夹
$y=date('Y',time());//年
$m=date('m',time());
if(!is_dir('../../../attachment/images/'.$y.$m)){
mkdir('../../../attachment/images/'.$y.$m,0777);//创建年+月的文件夹权限为最高
}
//获取图片路径并移动
$text=$file_url;//图片路径1
// $file_url;
// $file_url;
$text1=$file_name;//图片路径2
$text1;
rename(iconv("UTF-8","GBK",$text),iconv("UTF-8","GBK",$text1));
}
//------------------------------------------------------------------------------------------------把所有的数据添加到数据库
//新增数据
$uid=array();
$k_n=0;
foreach ($cd_info1 as $key => $value) {
// var_dump($value);
// 数据新增
$img_list= $cd_img[$k_n];
//循环存储详细信息的页面
$img_list1=null;
foreach ($img_list as $key3 => $value3) {
$img_list1=$img_list1."";
}
//------------------------------------------添加主图--------------------------------------------------
$lotimg=0;//定数器
foreach($cd_img[$k_n] as $key1 =>$value1){
$arr_1=explode('/',$value1);
$number1=$arr_1[count($arr_1)-1];
$number2=$arr_1[count($arr_1)-2];
$number = "../../../FTP_FILE/contentPic/".$number2.'/'.$number1;//获取图片真实路径
//判断图片的高度是否正常
// $aa=getimagesize($number);
// $width=$aa['0'];
$as= getimagesize(iconv("UTF-8","GBK",$number));
$height=$as["1"];///获取图片的高
// 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'],//团购价格
'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;
}
//--------------------------------------------------------------------------------------------图片存入数据库
var_dump($uid[0]);
if($uid[0]!=''){
$j_n=0;//自增变量
foreach ($cd_img as $key => $value) {
'';
foreach ($value as $key2 => $value2) {
$y=date('Y',time());$m=date('m',time());//文件夹名字需要的年月
$img_url="images/".$y.$m.'/'.basename($value2);
$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 '图片储存成功';
}
// $set = pdo_fetch("select * from " . tablename('tg_goods') . "where fk_typeid=".$value['class']."");
// $set['gname'].'';
//----------------------------------------------$cd_img是获取到的图片数组路径-执行循环移动图片----------------------------
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;//相对文件夹路径/xxxxxx/xxx.jpg
$img_name=basename($value1);//图片文件名字
$hostdir=dirname("../../../FTP_FILE/contentPic/..");//设置图片路径
//获取也就是扫描文件夹内的文件及文件夹名存入数组 $filesnames
$filesnames = scandir($hostdir); //文件名称目录
$y=date('Y',time());//年
$m=date('m',time());
$text= $hostdir.'/'.$url_name; //图片路径1
$text1="../../../attachment/images/".$y.$m."/".$img_name."";//图片路径2
hover_file($text,$text1);//执行移动文件函数
}
$arr_1='';
}
//-------------------------------------------------------------删除所有文件
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);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
deldir('../../../FTP_FILE/contentPic');//删除文件夹目录
$file1 = "../../../FTP_FILE/FILE1.xlsx";
$file2 = "../../../FTP_FILE/FILE2.xlsx";
unlink($file1);//删除文件1
unlink($file2);//删除文件2
echo "";
?>
以上就介绍了 读取两个excl文件和图片文件夹 并将其中的图片文件移动其他文件夹excl内容上传数据库,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。