PHP圖片上傳類

來源:互聯網
上載者:User

標籤:

前言
  在php開發中,必不可少要用到檔案上傳,整理封裝了一個圖片上傳的類也還有必要。
一、控制器調用
public function upload_file() {
if (IS_POST) {
if (!empty($_FILES[‘Filedata‘])) {
import(‘Org.Upload‘, COMMON_PATH);
$upload = new \Upload();
// 允許上傳檔案大小
$upload->allowMaxSize(C(‘ALLOW_UPLOAD_FILE_MAX_SIZE‘));
// 允許上傳的檔案類型
$upload->allowExt(C(‘ALLOW_UPLOAD_FILE_TYPE‘));
// 擷取上傳檔案的資訊
$upload->get_upload_file_info($_FILES[‘Filedata‘]);
// 指定上傳目錄
$upload->root_dir(ROOT_PATH);
// 組建檔案名
$file_name = $upload->upload_file_name();
// 儲存到指定的目錄
$res = $upload->save_file(‘Uploads/‘, $file_name);
if ($res === false) {
dump($upload->get_error());
} else {
echo ‘上傳成功‘;
}
}
}
}

二、檔案上傳類代碼

<?php

/**
* @desc 檔案上傳類
* @author Timothy
* Created by PhpStorm.
* Date: 2016/10/17
* Time: 12:18
*/
class Upload
{
protected $_error = ‘‘;
protected $_allowExt = array();
protected $_allowSize = 0;
protected $file_info = null;
protected $_root_dir = null;

/**
* @desc 上傳檔案資訊
* @param array $file_info
* @return bool
*/
public function get_upload_file_info(array $file_info = array()) {
if (!is_uploaded_file($file_info[‘tmp_name‘])) {
$this->_error = ‘檔案不是通過HTTP POST方式上傳的‘;
return false;
}
$this->file_info = $this->_format_upload_file_info($file_info);
}

/**
* @desc 格式化上傳檔案的資訊
* @param array $file_info
* @return array
*/
private function _format_upload_file_info(array $file_info = array()) {
$pathinfo = pathinfo($file_info[‘name‘], PATHINFO_EXTENSION);
$file_info[‘extension‘] = $pathinfo;
return $file_info;
}

// 上傳檔案的錯誤資訊處理
private function _checkError($file_error = ‘‘) {
switch ($file_error) {
case UPLOAD_ERR_INI_SIZE:
$this->_error = ‘上傳檔案超過了PHP設定檔中upload_max_filesize選擇項的值‘;
break;
case UPLOAD_ERR_FORM_SIZE:
$this->_error = ‘超過了表單MAX_FILE_SIZE限制的大小‘;
break;
case UPLOAD_ERR_PARTIAL:
$this->_error = ‘檔案部分被上傳‘;
break;
case UPLOAD_ERR_NO_FILE:
$this->_error = ‘沒有檔案被上傳‘;
break;
case UPLOAD_ERR_NO_TMP_DIR :
$this->_error = ‘找不到臨時檔案‘;
break;
case UPLOAD_ERR_CANT_WRITE:
$this->_error = ‘檔案寫入失敗‘;
break;
case UPLOAD_ERR_EXTENSION:
$this->_error = ‘檔案類型不正確‘;
break;
default:
$this->_error = ‘系統錯誤‘;
break;
}
}

/**
* @desc 判斷上傳檔案的類型
* @param string $file_type
* @return bool
*/
private function _checkExt($file_type = ‘‘) {
if (!in_array($file_type, $this->_allowExt)) {
$this->_error = ‘上傳的檔案類型不正確‘;
return false;
}
return true;
}

/**
* @desc 判斷上傳檔案的大小
* @param int $file_size
* @return bool
*/
private function _checkSize($file_size = 0) {
if ($file_size > $this->_allowSize) {
$this->_error = ‘上傳的圖片過大‘;
return false;
}
return true;
}

/**
* @desc 處理允許上傳的檔案類型
* @param int $max_size
* @return bool|int
*/
public function allowMaxSize($max_size = 2) {
if (!is_numeric($max_size)) {
$this->_error = ‘允許上傳的檔案大小不正確‘;
return false;
}
$this->_allowSize = $max_size * 1024 * 1024;
}

/**
* @desc 處理允許上傳的檔案類型
* @param string $ext
*/
public function allowExt($ext = ‘‘) {
$this->_allowExt = explode(‘|‘, $ext);
}

/**
* @desc 指定路徑
* @param none
* @return void
*/
public function root_dir($dir) {
$this->_root_dir = $dir;
}

/**
* @desc 遞迴建立目錄
* @param $path
* @return bool
*/
private function _mkdir($path) {
if (!is_dir($path)) {
if (@mkdir($path, 0777, true))
return true;
else
$this->_error = ‘目錄建立失敗‘;
return false;
} else {
return true;
}
}

// 產生一個唯一的檔案名稱,防止因重名而被覆蓋
public function upload_file_name() {
return md5(uniqid(microtime(true), true)) . ‘.‘ . $this->file_info[‘extension‘];
}

/**
* @desc 把上傳的臨時檔案儲存到指定目錄
* @param string $path
* @param string $file_name
* @return string
*/
public function save_file($path = ‘‘, $file_name = ‘‘) {
// 判斷錯誤號碼
if ($this->file_info[‘error‘] > 0) {
if (!$this->_checkError($this->file_info[‘error‘])) {
return false;
}
}

// 判斷是否是合法的檔案類型
if (!$this->_checkExt($this->file_info[‘extension‘])) {
return false;
}

// 判斷是否是合法的檔案大小
if (!$this->_checkSize($this->file_info[‘size‘])) {
return false;
}

if (!$this->_checkTrueImage($this->file_info[‘tmp_name‘])) {
return false;
}

$abs_path = $this->_root_dir ? $this->_root_dir . $path : $path ;
if ($this->_mkdir($abs_path)) {
if (move_uploaded_file($this->file_info[‘tmp_name‘], $abs_path . $file_name)) {
@chmod($abs_path, 0666);
return $abs_path;
}
} else {
$this->_error = ‘上傳檔案失敗‘;
}
}

/**
* @desc 判斷是否是真實的圖片
* @param string $file_info
* @return bool
*/
private function _checkTrueImage($file_info = ‘‘) {
if (!getimagesize($file_info)) {
$this->_error = ‘檔案不是真實的圖片‘;
return false;
}
return true;
}

/**
* @desc 擷取上傳錯誤資訊,然後返回
* @return string
*/
public function get_error() {
return $this->_error;
}
}

PHP圖片上傳類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.