thinkphp實(shí)現(xiàn)UploadFile.class.php圖片上傳效能
Jun 13, 2016 pm 12:01 PM
thinkphp實(shí)現(xiàn)UploadFile.class.php圖片上傳功能
1.我們首先需要?jiǎng)?chuàng)建一個(gè)表
CREATE TABLE IF NOT EXISTS `tp_image` (
? `id` int(11) NOT NULL AUTO_INCREMENT,
? `image` varchar(200) NOT NULL,
? `create_time` int(11) NOT NULL,
? PRIMARY KEY (`id`)
) ENGINE=MyISAM? DEFAULT CHARSET=utf8;
2.然后再conf文件里添加配置(最后一段配置是可選的,只是為了方便統(tǒng)一管理URL路徑)
return array(
??????? 'URL_MODEL'??? =>??? 2, // 如果你的環(huán)境不支持PATHINFO 請(qǐng)?jiān)O(shè)置為3
??????? 'DB_TYPE'??? =>??? 'mysql',
??????? 'DB_HOST'??? =>??? 'localhost',
??????? 'DB_NAME'??? =>??? 'thinkphp',
??????? 'DB_USER'??? =>??? 'root',
??????? 'DB_PWD'??? =>??? '',
??????? 'DB_PORT'??? =>??? '3306',
??????? 'DB_PREFIX'??? =>??? 'tp_',
??????? 'SHOW_PAGE_TRACE' =>true,??????? //顯示頁(yè)面調(diào)試明細(xì)
??????? 'TMPL_PARSE_STRING' =>? array( // 地址替換,用_UPLOAD_目錄 代替 根目錄下的Upload目錄
???????? '__UPLOAD__'??? =>? __ROOT__.'/Uploads',
???? ),
);
?>
3.添加一個(gè)Image模塊(名字可以隨便取)
??? class ImageAction extends Action{????????
??????? /**
???????? * 創(chuàng)建index 入口方法
???????? */
??????? public function index(){
??????????? $image=M('Image');???????????????????????
??????????? $data=$image->order('create_time desc')->find();??? //獲取最后上傳圖片
??????????? $this->assign('data',$data);
??????????? $this->display();
??????? }?
?>
4.創(chuàng)建相應(yīng)index視圖文件(index.html)
???
???


???
5.選擇圖片,點(diǎn)擊上傳按鈕后,會(huì)跳到Image模塊的upload方法上,Image模塊上現(xiàn)在還沒(méi)有這個(gè)方法,于是我們創(chuàng)建它
??? class ImageAction extends Action{????????
??????? /**
???????? * 創(chuàng)建index 入口方法
???????? */
??????? public function index(){
??????????? $image=M('Image');????????????????????????
??????????? $data=$image->order('create_time desc')->find();??? //獲取最后上傳圖片
??????????? var_dump($data);
??????????? $this->assign('data',$data);
??????????? $this->display();
??????? }?
??????? //如果上傳的文件不為空,跳轉(zhuǎn)到_upload方法
??????? public function upload(){????????
??????????? //如果不為空
??????????? if(!empty($_FILES))
??????????? {
??????????????? $this->_upload();
??????????? }
??????? }
6.如果提交的不是NULL,則跳到_upload方法上,此方法實(shí)現(xiàn)圖片上傳的功能
??? class ImageAction extends Action{????????
??????? /**
???????? * 創(chuàng)建index 入口方法
???????? */
??????? public function index(){
??????????? $image=M('Image');????????????????????????
??????????? $data=$image->order('create_time desc')->find();??? //獲取最后上傳圖片
??????????? var_dump($data);
??????????? $this->assign('data',$data);
??????????? $this->display();
??????? }?
????????
??????? //如果上傳的文件不為空,跳轉(zhuǎn)到_upload方法
??????? public function upload(){????????
??????????? //如果不為空
??????????? if(!empty($_FILES))
??????????? {
??????????????? $this->_upload();
??????????? }
??????? }
????????
??????? /***
???????? * 實(shí)現(xiàn)圖片上傳
???????? */
??????? public function _upload(){
??????????? import('@.ORG.UploadFile');
??????????? //導(dǎo)入上傳類
??????????? $upload = new UploadFile();
??????????? //設(shè)置上傳文件大小
??????????? $upload->maxSize??????????? = 3292200;
??????????? //設(shè)置上傳文件類型
??????????? $upload->allowExts????????? = explode(',', 'jpg,gif,png,jpeg');
??????????? //設(shè)置附件上傳目錄
??????????? $upload->savePath?????????? = './Uploads/';
??????????? //設(shè)置需要生成縮略圖,僅對(duì)圖像文件有效
??????????? $upload->thumb????????????? = true;
??????????? // 設(shè)置引用圖片類庫(kù)包路徑
??????????? $upload->imageClassPath???? = '@.ORG.Image';
??????????? //設(shè)置需要生成縮略圖的文件后綴
??????????? $upload->thumbPrefix??????? = 'm_,s_';? //生產(chǎn)2張縮略圖
??????????? //設(shè)置縮略圖最大寬度
??????????? $upload->thumbMaxWidth????? = '400,100';
??????????? //設(shè)置縮略圖最大高度
??????????? $upload->thumbMaxHeight???? = '400,100';
??????????? //設(shè)置上傳文件規(guī)則
??????????? $upload->saveRule?????????? = 'uniqid';
??????????? //刪除原圖
??????????? $upload->thumbRemoveOrigin? = true;
????????????
??????????? //如果上傳不成功
??????????? if (!$upload->upload())?
??????????? {
??????????????? //捕獲上傳異常
??????????????? $this->error($upload->getErrorMsg());
??????????? }?
??????????? else?
??????????? {
??????????????? //取得成功上傳的文件信息
??????????????? $uploadList = $upload->getUploadFileInfo();
????????????????
??????????????? //導(dǎo)入圖片類
??????????????? import('@.ORG.Image');????????????????
??????????????? //給m_縮略圖添加水印, Image::water('原文件路徑','水印圖片地址')
??????????????? Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], APP_PATH.'Tpl/Public/Images/logo.png');
??????????????? //圖片名賦值給 字段image
??????????????? $_POST['image'] = $uploadList[0]['savename'];
??????????? }
??????????? $model? = M('image');
??????????? //保存當(dāng)前數(shù)據(jù)對(duì)象
??????????? $data['image']????????? = $_POST['image'];
??????????? $data['create_time']??? = NOW_TIME;
??????????? $list?? = $model->add($data);
??????????? if ($list !== false)?
??????????? {
??????????????? $this->success('上傳圖片成功!');
??????????? }?
??????????? else?
??????????? {
??????????????? $this->error('上傳圖片失敗!');
??????????? }
??????? }????????
??? }
?>
上傳成功生成兩張縮略圖
需要說(shuō)明的是:
ThinkPHP里自帶的圖片上傳類(UploadFile.class.php) 和圖片模型類(Image.class.php),要完整版的ThinkPHP包才有。
沒(méi)有的話需要在Lib里創(chuàng)建一個(gè)文件夾(ORG),然后去官網(wǎng)下載擴(kuò)展包把這兩個(gè)文件放到ORG文件夾中。
我的是第二種情況

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using
