$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."
以上就介绍了 读取两个excl文件和图片文件夹 并将其中的图片文件移动其他文件夹excl内容上传数据库,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。