ThinkPHP is a popular lightweight PHP framework in China. One of the most popular factors in China is that its instruction documents are sound and complete, and the comments in its source code are all Chinese, it is easier for programmers with poor English skills to learn.
Image Upload is a common function on websites. ThinkPHP also has its own Image Upload class (UploadFile. class. php) and Image model class (Image. class. php ). It is convenient for us to implement the image upload function.
1. Create a table first.
CREATE TABLE `id` int(11) NOT AUTO_INCREMENT, `image` varchar(200) NOT , `create_time` int(11) NOT , PRIMARY ) ENGINE=MyISAM CHARSET=utf8;
2. Then add the configuration in the conf file (the last configuration is optional, just to facilitate unified management of the URL path)
<? 'URL_MODEL' => 2, 'DB_TYPE' => 'mysql', 'DB_HOST' => 'localhost', 'DB_NAME' => 'thinkphp', 'DB_USER' => 'root', 'DB_PWD' => '', 'DB_PORT' => '3306', 'DB_PREFIX' => 'tp_', 'SHOW_PAGE_TRACE' =>, 'TMPL_PARSE_STRING' => ( '__UPLOAD__' => __ROOT__.'/Uploads',,?>
3. Add an Image module (the name can be obtained at will)
<? ImageAction =M('Image'=->order('create_time desc')->find(); ->assign('data',->?>
4.create the corresponding indexvideo file (index.html)
<! DOCTYPE html "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd ">
5. Select an Image and click the upload button. It will jump to the upload method of the Image module. This method is not available in the Image module, So we create it.
<? ImageAction =M('Image'=->order('create_time desc')->find(); (->assign('data',-> (!(->
6. If the submitted object is not NULL, jump to the _ upload method. This method uploads images.
<? ImageAction = M ('image' =-> order ('create _ time desc ')-> find (); (-> assign ('data',-> (! (-> '@. ORG. uploadFile '=-> maxSize = 3292200-> allowExts = (', ', 'jpg, gif, png, jpeg'-> savePath = '. /Uploads/'-> thumb =-> imageClassPath = '@. ORG. imag'-> thumbPrefix ='m _, s _'; -> thumbMaxWidth = '000000'-> thumbMaxHeight = '000000'-> saveRule = 'uniqid'-> thumbRemoveOrigin = (! -> Error (-> =-> import ('@. ORG. image 'image: water ([0] ['savepath'].'m _'. [0] ['savename'], APP_PATH. 'tpl/Public/Images/logo.png '['image'] = [0] ['savename' = M ('image' ['image'] = ['image '[' create _ time'] =-> add ((! ==> Success ('image uploaded successfully! '-> Error (' Image Upload Failed! '?>
After the final image is uploaded successfully, two thumbnails are generated:
Note:
The built-in Image Upload class (UploadFile. class. php) and Image model class (Image. class. php) in ThinkPHP require the full version of the ThinkPHP package.
If not, create a folder (ORG) in Lib and download the extension package from the official website to put the two files in the ORG folder.
The second case is mine.