批改狀態(tài):合格
老師批語:你的命名很規(guī)范, 語義化很好
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上傳文件</title> </head> <body> <form action="demo1.php" method="post" enctype="multipart/form-data"> <!--設(shè)置上傳文件大小最大為3M 這段代碼一定要放到文件提交框之前--> <!-- 1M = 1024K = 1024*1024Byte=1048576Byte--> <input type="hidden" name="MAX_FILE_SIZE" value="3145728"> <input type="file" name="my_file" id=""> <!--button默認(rèn)為提交按鈕--> <button>上傳</button> </form> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php // 自定義異常類 class CustomException extends Exception { public function errorInfo() { $str="<h3> <strong>錯(cuò)誤代碼:{$this->getCode()} --- </strong> <span style='color: red'>{$this->getMessage()}</span> </h3>"; return $str; } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php require 'CustomException.php'; //引入CustomException.php class FUpload { public $flie; // 原始文件名稱 protected $fileName; // 臨時(shí)文件名 protected $fileTmpName; //文件類型 protected $fileType; //文件錯(cuò)誤代碼 protected $fileError; //文件大小 protected $fileSize; //文件保存路徑 protected $savePath; //保存的文件名 protected $saveFileName; // 允許上傳的文件后綴 protected $allowExts=['jpg','jpeg','png','gif']; public function __construct($file,$savePath='') { $this->file=$file; $this->fileName=$file['name']; $this->fileTmpName=$file['tmp_name']; $this->fileType=$file['type']; $this->fileError=$file['error']; $this->fileSize=$file['size']; $this->savePath=$savePath; if (!file_exists($savePath)){ mkdir($savePath,2019,true);//創(chuàng)建多級目錄 } } public function __get($name) { return $this->$name; } public function __set($name, $value) { $this->$name=$value; } //文件上傳 public function upload() { // 判斷是否上傳成功 $this->getFileError(); //判斷文件后綴名是否符合要求 $extension=$this->getExtension(); $this->saveFileName=date('YmdHis',time()).mt_rand(1,99).'.'.$extension; if(is_uploaded_file($this->fileTmpName)){ if (move_uploaded_file($this->fileTmpName,$this->savePath.$this->saveFileName)){ echo '<script>alert("上傳成功");history.back();</script>'; }else{ throw new CustomException('文件無法移動(dòng)到指定目錄, 請檢查目錄的寫權(quán)限',103); } }else{ throw new CustomException('非法操作',102); } } //判斷是否上傳成功 protected function getFileError() { if($this->fileError>0){ switch ($this->fileError){ case 1: throw new CustomException('上傳的文件超過了 php.ini 中 upload_max_filesize選項(xiàng)限制的值。',1); case 2: throw new CustomException('上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。上傳文件不允許超過3M',2); case 3: throw new CustomException('文件只有部分被上傳。上傳文件不完整',3); case 4: throw new CustomException('沒有文件被上傳',4); case 6: throw new CustomException('找不到臨時(shí)文件夾',6); case 7: throw new CustomException('文件寫入失敗',7); default: throw new CustomException('未知錯(cuò)誤',8); } } } //判斷文件后綴名是否符合要求 protected function getExtension() { $myfile=explode('.',$this->fileName); $extension=array_pop($myfile); if(!in_array($extension,$this->allowExts)){ throw new CustomException('不允許上傳'.$extension.'文件類型',101); } return $extension; } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php require 'FUpload.php'; try{ $obj=new FUpload($_FILES['my_file'],'uploads/img/'); $obj->upload(); }catch (CustomException $e){ echo $e->errorInfo(); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號