日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

PHP的上传文件思路及其代码

發(fā)布時(shí)間:2024/9/19 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP的上传文件思路及其代码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

思路

1.驗(yàn)證上傳的數(shù)據(jù):文件是否存在file_exists? ;文件大小;是否為真的圖片;

2.驗(yàn)證文件格式? ?:$file_type = strtolower( end( $tmp_file_extend ) );in_array

3.檢驗(yàn) is_uploaded_file() 函數(shù)檢查指定的文件是否是通過(guò) HTTP POST 上傳的。

4.沒有對(duì)應(yīng)文件夾的話? 新建文件夾與復(fù)制權(quán)限

if ( ! file_exists( $upload_dir ) ){mkdir( $upload_dir,0777);chmod( $upload_dir,0777); }

5.移動(dòng) move_uploaded_file() 函數(shù)將上傳的文件移動(dòng)到新位置。

例子1

1.代碼:獲取處理參數(shù)? 保存至數(shù)據(jù)庫(kù)與文件夾

<?php namespace app\common\services;use app\models\book\Images; use Yii;class UploadService extends BaseService {public static function uploadByFile ($file_name,$file_path,$bucket) {if ( !$file_name ) {return self::_err("參數(shù)文件名是必須的");}if ( !$file_path || !file_exists( $file_path )) {return self::_err("參數(shù)文件名是必須的");}$upload_config = Yii::$app->params['upload'];if (!isset ($upload_config[$bucket])) {return self::_err("指定參數(shù)籃子錯(cuò)誤");}$tmp_file_extend = explode(".",$file_name);$file_type = strtolower( end( $tmp_file_extend ) );$hash_key = md5 (file_get_contents($file_path));$upload_dir_path = UtilService::getRootPath() . "/web" . $upload_config[ $bucket ].'/';$folder_name = date ("Ymd");$upload_dir = $upload_dir_path.$folder_name;if ( ! file_exists( $upload_dir ) ){mkdir( $upload_dir,0777);chmod( $upload_dir,0777);}$upload_file_name = $folder_name."/".$hash_key.".{$file_type}";if ( is_uploaded_file( $file_path ) ){move_uploaded_file($file_path,$upload_dir_path.$upload_file_name);}else{file_put_contents( $upload_dir_path.$upload_file_name,file_get_contents( $file_path ) );}self::saveImage( $bucket,$upload_file_name );return ['code' => 200,'path' => $upload_file_name,'prefix' => $upload_config[ $bucket ] ."/"];}public static function saveImage($bucket = '' ,$file_key = ''){$images = new Images();$images->bucket = $bucket;$images->file_key = $file_key;$images->created_time = date("Y-m-d H:i:s",time());return $images->save();}}

2.調(diào)用:控制器就是在處理參數(shù)? 在調(diào)用服務(wù)保存

public function actionPic(){$bucket = trim($this->post("bucket"));$callback = "window.parent.upload";if ( !$_FILES || !isset($_FILES['pic']) ){return "<script>{$callback}.error('請(qǐng)選擇文件之后在提交')</script>";}$file_name = $_FILES['pic']['name'];$tmp_file_extend = explode(".",$file_name);if ( !in_array( strtolower ( end ( $tmp_file_extend ) ),$this->allow_file_type ) ) {return "<script>{$callback}.error('請(qǐng)上傳指定類型的文件')</script>";}//todo upload function$ret = UploadService::uploadByFile( $file_name,$_FILES['pic']['tmp_name'],$bucket);if ( ! $ret ) {return "<script>{$callback}.error('".UploadService::getLastErrorMsg()."')</script>";} else {return "<script>{$callback}.success('{$ret['path']}')</script>";}}

例子2

PHP上傳類

各種檢測(cè)后? 進(jìn)行把上傳的文件移動(dòng)到目標(biāo)位置處

<?php //$fileInfo=$_FILES['myFile']; function uploadFile($fileInfo,$uploadPath = 'uploads',$flag=true,$allowExt=array('jpeg','jpg','gif','png'),$maxSize = 2097152){// 判斷錯(cuò)誤號(hào)if ($fileInfo ['error'] > 0) {switch ($fileInfo ['error']) {case 1 :$mes = '上傳文件超過(guò)了PHP配置文件中upload_max_filesize選項(xiàng)的值';break;case 2 :$mes = '超過(guò)了表單MAX_FILE_SIZE限制的大小';break;case 3 :$mes = '文件部分被上傳';break;case 4 :$mes = '沒有選擇上傳文件';break;case 6 :$mes = '沒有找到臨時(shí)目錄';break;case 7 :case 8 :$mes = '系統(tǒng)錯(cuò)誤';break;}echo ( $mes );return false;}$ext = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION ); // $allowExt = array ( // 'jpeg', // 'jpg', // 'png', // 'gif' // );if(!is_array($allowExt)){exit('系統(tǒng)錯(cuò)誤');}// 檢測(cè)上傳文件的類型if (! in_array ( $ext, $allowExt )) {exit ( '非法文件類型' );}//$maxSize = 2097152; // 2M// 檢測(cè)上傳文件大小是否符合規(guī)范if ($fileInfo ['size'] > $maxSize) {exit ( '上傳文件過(guò)大' );}//檢測(cè)圖片是否為真實(shí)的圖片類型//$flag=true; if($flag){if(!getimagesize($fileInfo['tmp_name'])){exit('不是真實(shí)圖片類型');}}// 檢測(cè)文件是否是通過(guò)HTTP POST方式上傳上來(lái)if (! is_uploaded_file ( $fileInfo ['tmp_name'] )) {exit ( '文件不是通過(guò)HTTP POST方式上傳上來(lái)的' );}//$uploadPath = 'uploads';if (! file_exists ( $uploadPath )) {mkdir ( $uploadPath, 0777, true );chmod ( $uploadPath, 0777 );}$uniName = md5 ( uniqid ( microtime ( true ), true ) ) . '.' . $ext;$destination = $uploadPath . '/' . $uniName;if (! @move_uploaded_file ( $fileInfo ['tmp_name'], $destination )) {exit ( '文件移動(dòng)失敗' );}//echo '文件上傳成功'; // return array( // 'newName'=>$destination, // 'size'=>$fileInfo['size'], // 'type'=>$fileInfo['type'] // );return $destination; }

PHP調(diào)用類

<?php header('content-type:text/html;charset=utf-8'); include_once 'upload.func.php'; $fileInfo=$_FILES['myFile']; // $newName=uploadFile($fileInfo); // echo $newName; // $newName=uploadFile($fileInfo,'imooc'); // echo $newName; //$allowExt='txt'; $allowExt=array('jpeg','jpg','png','gif','html','txt'); $newName=uploadFile($fileInfo,'imooc',false,$allowExt); echo $newName;

總結(jié)

以上是生活随笔為你收集整理的PHP的上传文件思路及其代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。