php excl图片上传,php 使用PHPExcel 拓展上传文件
public static function ImportExcel($door_id)
{
set_time_limit(0); //解決上傳超時
@ini_set('memory_limit', '512M'); // 解決上傳內存不夠
$self = new self();
//導入excel
$addFile=$_FILES['file'];
if (empty($addFile['name'])){
return ['err'=> 1,'msg' =>'請上傳表格'];
}
$excelFileName =$addFile['tmp_name'];
$file_types = explode ( ".", $addFile['name'] );
$excel_type = array('xlsx');
if($addFile['error'] > 0){
return ['err' => 1,'msg'=>'上傳出現錯誤,錯誤代碼'.$addFile['error']];
}
if (!in_array(strtolower(end($file_types)),$excel_type)){
return ['err'=> 2,'msg' =>'文件格式錯誤,只允許上傳xlsx文件'];
}
include_once dirname(dirname(dirname(__FILE__))).'/vendor/Classes/PHPExcel/IOFactory.php';
include_once dirname(dirname(dirname(__FILE__))).'/vendor/Classes/PHPExcel/Cell.php';
$objPHPExcel = \PHPExcel_IOFactory::load($excelFileName);
$sheet = $objPHPExcel->getSheet(0);
$data=$sheet->toArray();
$highestColumn = $sheet->getHighestColumn();
$highestRow = $sheet->getHighestRow(); // 取得總行數
$imageFilePath='static/excel/media/'.$door_id.'/';//圖片在本地存儲的路徑
if (!file_exists ( $imageFilePath )){
mkdir("$imageFilePath", 0777, true);
}
//excel 中帶有圖片
// foreach($sheet->getDrawingCollection() as $img){
// list($startColumn,$startRow)= \PHPExcel_Cell::coordinateFromString($img->getCoordinates());//獲取圖片所在行和列
// $imageFileName = $img->getCoordinates() . mt_rand(100, 999);
// copy($img->getPath(),$imageFilePath.$imageFileName.'.'.$img->getExtension());
// //插入代碼
// $startColumn = self::ABC2decimal($startColumn);//由于圖片所在位置的列號為字母,轉化為數字
// $data[$startRow-1][$startColumn]=$imageFilePath.$imageFileName.'.'.$img->getExtension();//把圖片插入到數組中
// }
$columns = self::ABC2decimal($highestColumn);
$str = '';
// $industry = MediaIndustry::find()->select('id,industry_name')->where(['media_id' => 1])->asArray()->all();
$industry = MediaIndustry::find()->select('id,industry_name')->asArray()->all();
$indu = ArrayHelper::map($industry,'id','industry_name');
$transaction = Yii::$app->db->beginTransaction();
for ($row = 1; $row < $highestRow; $row++){
for ($col = 0; $col <= $columns ; $col++) {
$str = $data[0][$col];
$media = new Media; //判斷excel 表格中的數據
switch ($str) {
case 'industry_id':
$media->industry_id = (int)array_search($data[$row][$col],$indu);
break;
case 'region_id':
// $media->industry_id = array_search($data[$row][$col],$indu);
break;
case 'link_type':
$media->link_type = array_search($data[$row][$col],$self->link);
break;
case 'entrance_level':
$media->entrance_level = array_search($data[$row][$col],$self->entrance_level);
break;
case 'news_source':
$media->news_source = array_search($data[$row][$col],$self->news_sources);
break;
default:
$media->$str = (string)$data[$row][$col];
break;
}
}
if(!$media->save()){
$transaction->rollback();
return ['err'=> 1,'msg' =>'請檢查第'.$row.'行'];
}
}
unset($media);
unset($data);
$transaction->commit();
return ['err'=> 0,'msg' =>'導入成功'];
}
/**
* 文件導入時由于圖片所在位置的列號為字母,轉化為數字
* @param [type] $abc [description]
*/
public static function ABC2decimal($abc)
{
$ten = 0;
$len = strlen($abc);
for($i=1;$i<=$len;$i++){
$char = substr($abc,0-$i,1);//反向獲取單個字符
$int = ord($char);
$ten += ($int-65)*pow(26,$i-1);
}
return $ten;
}
標簽:media,industry,data,col,PHPExcel,php,上傳,id,row
來源: https://www.cnblogs.com/wangsaii/p/13152823.html
總結
以上是生活随笔為你收集整理的php excl图片上传,php 使用PHPExcel 拓展上传文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java antd实现登录,基于 ant
- 下一篇: 张元干故居简介(张元干的诗词全集)