国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

php上傳文件代碼不足與改進

瀏覽:90日期:2022-06-06 10:44:53

問題描述

<?php /** * 單文件上傳 */ // 單文件上傳 class UploadFile { /** * 構造函數 * @param [File] $file 文件對象 */ function __construct($file) { $this->upload_status = ''; $this->upload_message = ''; $this->file = $file; $this->mime_map = array('.jpeg' => array('image/jpeg', 'image/pjpeg'),'.jpg' => array('image/jpeg', 'image/pjpeg'),'.png' => array('image/png', 'image/x-png'),'.gif' => array('image/gif') ); $this->allow_ext_list = array('.jpeg', '.jpg', '.png', '.gif'); $this->upload_path = './upload/'; $this->checkFileParam(); $this->checkFileError(); $this->checkFileType(); $this->uploadStart(); } /** * 驗證是否傳入了正確的文件對象 * 必須包含[name, type, size, error, tmp_name] */ private function checkFileParam() { if ($this->upload_status === 'error') return; $file = $this->file; $right_name = gettype($file['name']) === 'string'; $right_type = gettype($file['type']) === 'string'; $right_tmp_name = gettype($file['tmp_name']) === 'string'; $right_error = gettype($file['error']) === 'integer'; $right_size = gettype($file['size']) === 'integer'; if (!$right_name || !$right_type || !$right_tmp_name || !$right_error || !$right_size) {$this->upload_message = '傳入的文件對象參數不合法!';$this->upload_status = 'error'; } } // 檢查文件對象接收的錯誤信息 private function checkFileError() { if ($this->upload_status === 'error') return; $error = $this->file['error']; if ($error == 0) return; switch ($error) {case 1: $upload_message = '上傳的文件超過了php.ini 中 upload_max_filesize 選項限制的值!'; break;case 2: $upload_message = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值!'; break;case 3: $upload_message = '文件只有部分被上傳!'; break;case 4: $upload_message = '沒有文件被上傳!'; break;case 6: $upload_message = '找不到臨時文件夾!'; break;case 7: $upload_message = '文件寫入失敗!'; break;default: $upload_message = '未知錯誤!'; break; } $this->upload_status = 'error'; $this->upload_message = $upload_message; } // 檢查上傳的文件 MIME 類型是否合法 private function checkFileType() { if ($this->upload_status === 'error') return; $type = $this->file['type']; $name = $this->file['name']; $tmp_name = $this->file['tmp_name']; $allow_ext_list = $this->allow_ext_list; $mime_map = $this->mime_map; // 后綴檢查 $ext_name = strtolower(strrchr($name, '.')); if (!in_array($ext_name, $allow_ext_list)) {$this->upload_status = 'error';$this->upload_message = '上傳的文件類型不合法!';return; } // 瀏覽器提供信息堅持 $allow_mime_list = array(); foreach($allow_ext_list as $val) {$allow_mime_list = array_merge($allow_mime_list, $mime_map[$val]); } $allow_mime_list = array_unique($allow_mime_list); if (!in_array($type, $allow_mime_list)) {$this->upload_status = 'error';$this->upload_message = '上傳的文件類型不合法!';return; } // php自身檢查 // 需要開啟 extension=fileinfo $file_mime = new Finfo(FILEINFO_MIME_TYPE); $mime = $file_mime->file($tmp_name); if (!in_array($mime, $allow_mime_list)) {$this->upload_status = 'error';$this->upload_message = '上傳的文件類型不合法!';return; } } // 文件上傳 private function uploadStart() { if ($this->upload_status === 'error') return; $name = $this->file['name']; $tmp_name = $this->file['tmp_name']; $ext_name = strtolower(strrchr($name, '.')); $base_name = 'UPLOAD_' . md5(uniqid()); $upload_path = $this->upload_path; // 上傳路徑檢查 if (!is_dir($upload_path)) {mkdir($upload_path); } // is_uploaded_file($file) 檢查指定的文件是否是通過 HTTP POST 上傳的 // 如果文件是通過 HTTP POST 上傳的,該函數返回 TRUE。 if (is_uploaded_file($tmp_name)) {// move_uploaded_file($file, $path) 函數將上傳的文件移動到新位置。// 若成功,則返回 true,否則返回 false。if (move_uploaded_file($tmp_name, $upload_path . $base_name . $ext_name)) { $this->upload_status = 'success'; $this->upload_message = '文件上傳成功!'; return ;} else { // 需要寫入權限 $this->upload_message = '無法移動文件到指定位置!';} } else {$this->upload_message = '文件必須通過 HTTP POST 方式上傳!'; } $this->upload_status = 'error'; } }?>

問題解答

回答1:

格式亂了,如何編輯話題呢?

相關文章:
主站蜘蛛池模板: 天堂在线视频网站 | 国产成人午夜精品5599 | 午夜国产视频 | 国产精品亚洲片在线观看不卡 | 亚洲永久免费 | 九九热精 | 亚洲视频精选 | 激情6月丁香婷婷色综合 | 中文字幕在线看片成人 | 在线视频中文 | 亚洲国产欧美在线人成 | 黄色国产在线观看 | 久久怡红院亚欧成人影院 | 亚洲高清视频网站 | a一级特黄日本大片 s色 | 国产午夜在线观看视频播放 | 一本伊大人香蕉高清在线观看 | www成人国产在线观看网站 | 亚洲在线成人 | 欧美在线国产 | 91热播| 夜色邦合成福利网站 | 日韩精品一区二区三区乱码 | 一区二区播放 | 国产爱啪啪| 亚洲精品视频免费在线观看 | 欧美做爰性欧美 | 波多野结衣中文一区二区免费 | 美女被免费视频的网站 | 成人男女18免费o | 亚洲欧美精品国产一区色综合 | 欧美一级片在线看 | 三级视频在线播放线观看 | 成人a网站 | 亚洲国产91| 久久免费福利 | 国产日韩精品欧美一区 | 亚洲视频网站在线观看 | 欧美一级特黄aa大片视频 | 欧美一二三 | 99久久久精品免费观看国产 |