thinkphp implements UploadFile.class.php image upload function
Image uploading is a very common function in 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. Here is the implementation method
1. We first need to create a table
Copy codeThe code is as follows:
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. Then add the configuration in the conf file (the last configuration is optional, just for the convenience of unified management of URL paths)
Copy codeThe code is as follows:
return array(
'URL_MODEL' => 2, // If your environment does not support PATHINFO, please set it to 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, //Show page debugging details
'TMPL_PARSE_STRING' => array( // Address replacement, use _UPLOAD_ directory instead of the Upload directory in the root directory
'__UPLOAD__' => __ROOT__.'/Uploads',
),
);
?>
3. Add an Image module (you can choose any name)
Copy codeThe code is as follows:
class ImageAction extends Action{
/**
* Create index entry method
*/
public function index(){
$image=M('Image');
$data=$image->order('create_time desc')->find(); //Get the last uploaded image
$this->assign('data',$data);
$this->display();
}
?>
4. Create the corresponding index view file (index.html)
Copy codeThe code is as follows:
Insert title here
#img{height:22px; border:#000 2px solid}
#button{height:30px; width:100px;}
Upload allowed file types: gif png jpg image files, and generate 2 thumbnails, the large image is watermarked, and the original image will be deleted after generation.
5. Select the image and click the upload button, it will jump to the upload method of the Image module. There is no such method on the Image module yet, so we create it
Copy codeThe code is as follows:
class ImageAction extends Action{
/**
* Create index entry method
*/
public function index(){
$image=M('Image');
$data=$image->order('create_time desc')->find(); //Get the last uploaded image
var_dump($data);
$this->assign('data',$data);
$this->display();
}
//If the uploaded file is not empty, jump to the _upload method
public function upload(){
//If not empty
if(!empty($_FILES))
{
$this->_upload();
}
}
6. If the submission is not NULL, jump to the _upload method. This method implements the image upload function
Copy codeThe code is as follows:
class ImageAction extends Action{
/**
* Create index entry method
*/
public function index(){
$image=M('Image');
$data=$image->order('create_time desc')->find(); //Get the last uploaded image
var_dump($data);
$this->assign('data',$data);
$this->display();
}
//If the uploaded file is not empty, jump to the _upload method
public function upload(){
//If not empty
if(!empty($_FILES))
{
$this->_upload();
}
}
/***
* Implement image upload
*/
public function _upload(){
import('@.ORG.UploadFile');
//Import upload class
$upload = new UploadFile();
//Set upload file size
$upload->maxSize = 3292200;
//Set upload file type
$upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
//Set the attachment upload directory
$upload->savePath = './Uploads/';
//The settings need to generate thumbnails, which are only valid for image files
$upload->thumb = true;
//Set the reference image library package path
$upload->imageClassPath = '@.ORG.Image';
//Set the file suffix for which thumbnails need to be generated
$upload->thumbPrefix = 'm_,s_'; //Produce 2 thumbnails
//Set the maximum width of thumbnails
$upload->thumbMaxWidth = '400,100';
//Set the maximum height of thumbnails
$upload->thumbMaxHeight = '400,100';
//Set upload file rules
$upload->saveRule = 'uniqid';
//Delete original image
$upload->thumbRemoveOrigin = true;
//If the upload is unsuccessful
if (!$upload->upload())
{
//Catch upload exception
$this->error($upload->getErrorMsg());
}
else
{
//Get successfully uploaded file information
$uploadList = $upload->getUploadFileInfo();
//Import picture class
import('@.ORG.Image');
//Add watermark to m_thumbnail, Image::water('Original file path', 'Watermark image address')
Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], APP_PATH.'Tpl/Public/Images/logo.png');
//Assign the image name to the field image
$_POST['image'] = $uploadList[0]['savename'];
}
$model = M('image');
//Save the current data object
$data['image'] = $_POST['image'];
$data['create_time'] = NOW_TIME;
$list = $model->add($data);
if ($list !== false)
{
$this->success('Uploaded picture successfully!');
}
else
{
$this->error('Failed to upload image!');
}
}
}
?>
The upload was successful and two thumbnails were generated
What needs to be explained is:
The image upload class (UploadFile.class.php) and image model class (Image.class.php) that come with ThinkPHP require the full version of the ThinkPHP package.
If not, you need to create a folder (ORG) in Lib, then go to the official website to download the expansion package and put these two files into the ORG folder.
Mine is the second situation
http://www.bkjia.com/PHPjc/847863.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847863.htmlTechArticlethinkphp implements the UploadFile.class.php image upload function. Image upload is a very common function in websites. It is also available in ThinkPHP The built-in image upload class (UploadFile.class.php) and image model class...