PHP的上传文件思路及其代码
生活随笔
收集整理的這篇文章主要介紹了
PHP的上传文件思路及其代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路
1.驗證上傳的數據:文件是否存在file_exists? ;文件大小;是否為真的圖片;
2.驗證文件格式? ?:$file_type = strtolower( end( $tmp_file_extend ) );in_array
3.檢驗 is_uploaded_file() 函數檢查指定的文件是否是通過 HTTP POST 上傳的。
4.沒有對應文件夾的話? 新建文件夾與復制權限
if ( ! file_exists( $upload_dir ) ){mkdir( $upload_dir,0777);chmod( $upload_dir,0777); }5.移動 move_uploaded_file() 函數將上傳的文件移動到新位置。
例子1
1.代碼:獲取處理參數? 保存至數據庫與文件夾
<?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("參數文件名是必須的");}if ( !$file_path || !file_exists( $file_path )) {return self::_err("參數文件名是必須的");}$upload_config = Yii::$app->params['upload'];if (!isset ($upload_config[$bucket])) {return self::_err("指定參數籃子錯誤");}$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.調用:控制器就是在處理參數? 在調用服務保存
public function actionPic(){$bucket = trim($this->post("bucket"));$callback = "window.parent.upload";if ( !$_FILES || !isset($_FILES['pic']) ){return "<script>{$callback}.error('請選擇文件之后在提交')</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('請上傳指定類型的文件')</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上傳類
各種檢測后? 進行把上傳的文件移動到目標位置處
<?php //$fileInfo=$_FILES['myFile']; function uploadFile($fileInfo,$uploadPath = 'uploads',$flag=true,$allowExt=array('jpeg','jpg','gif','png'),$maxSize = 2097152){// 判斷錯誤號if ($fileInfo ['error'] > 0) {switch ($fileInfo ['error']) {case 1 :$mes = '上傳文件超過了PHP配置文件中upload_max_filesize選項的值';break;case 2 :$mes = '超過了表單MAX_FILE_SIZE限制的大小';break;case 3 :$mes = '文件部分被上傳';break;case 4 :$mes = '沒有選擇上傳文件';break;case 6 :$mes = '沒有找到臨時目錄';break;case 7 :case 8 :$mes = '系統錯誤';break;}echo ( $mes );return false;}$ext = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION ); // $allowExt = array ( // 'jpeg', // 'jpg', // 'png', // 'gif' // );if(!is_array($allowExt)){exit('系統錯誤');}// 檢測上傳文件的類型if (! in_array ( $ext, $allowExt )) {exit ( '非法文件類型' );}//$maxSize = 2097152; // 2M// 檢測上傳文件大小是否符合規范if ($fileInfo ['size'] > $maxSize) {exit ( '上傳文件過大' );}//檢測圖片是否為真實的圖片類型//$flag=true; if($flag){if(!getimagesize($fileInfo['tmp_name'])){exit('不是真實圖片類型');}}// 檢測文件是否是通過HTTP POST方式上傳上來if (! is_uploaded_file ( $fileInfo ['tmp_name'] )) {exit ( '文件不是通過HTTP POST方式上傳上來的' );}//$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 ( '文件移動失敗' );}//echo '文件上傳成功'; // return array( // 'newName'=>$destination, // 'size'=>$fileInfo['size'], // 'type'=>$fileInfo['type'] // );return $destination; }PHP調用類
<?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;總結
以上是生活随笔為你收集整理的PHP的上传文件思路及其代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在哪里能买到便宜点的腌咸菜缸?
- 下一篇: 美国东部时间现在几点(美国东部时间)