日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php excl图片上传,php 使用PHPExcel 拓展上传文件

發布時間:2024/9/19 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 拓展上传文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。