thinkphp realize picture upload function share _php instance

Source: Internet
Author: User
Tags create index

1. We first need to create a table

Copy Code code as follows:

CREATE TABLE IF not EXISTS ' Tp_image ' (
' id ' int (one) not NULL auto_increment,
' Image ' varchar not NULL,
' Create_time ' int (one) is 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 to facilitate the unified management of the URL path)

Copy Code code as follows:

<?php
Return Array (
' Url_model ' => 2,//If your environment does not support PathInfo please set 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,//Display page debug details

' Tmpl_parse_string ' => Array (//address replacement, with _upload_ directory instead of the UPLOAD directory under the root directory
' __upload__ ' => __root__. ' /uploads ',
),
);
?>

3. Add an image module (name can be arbitrarily taken)

Copy Code code as follows:

<?php
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 picture
$this->assign (' data ', $data);
$this->display ();
}
?>

4. Create the corresponding index view file (index.html)

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<style type= "Text/css" >
#img {height:22px border: #000 2px solid}
#button {height:30px; width:100px;}
</style>
<body>
<div class= "result" > Upload allowed file type: gif png jpg image file, and generate 2 thumbnails, where the large image with watermark, after the build will delete the original artwork. </div><br>
<notempty name= "Data" > </notempty>
<form action= "__url__/upload" method= "post" enctype= "Multipart/form-data" >
<input type= "file" name= "image" Id= "img"/>
<input type= "Submit" value= "Upload" id= "button" >
</form>
</body>

5. Select the picture, click the Upload button, will jump to the image module upload method, the image module does not have this method now, so we create it

Copy Code code as follows:

<?php
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 picture

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, then jump to the _upload method, this method to achieve the image upload function

Copy Code code as follows:

<?php
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 picture

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 ();
}

}


/***
* Implementation of 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 up attachment upload directory
$upload->savepath = './uploads/';
Settings need to generate thumbnails, valid only for image files
$upload->thumb = true;
Set reference Picture Class Library package path
$upload->imageclasspath = ' @.org.image ';
Set the file suffix to generate thumbnails
$upload->thumbprefix = ' m_,s_ '; Produce 2 thumbnail pictures
To set the maximum width of a thumbnail
$upload->thumbmaxwidth = ' 400,100 ';
To set the maximum height of a thumbnail
$upload->thumbmaxheight = ' 400,100 ';
Set upload file rules
$upload->saverule = ' uniqid ';
Delete Artwork
$upload->thumbremoveorigin = true;


If the upload is unsuccessful
if (! $upload->upload ())
{
Capture Upload exception
$this->error ($upload->geterrormsg ());
}
Else
{
Get file information for successful uploads
$uploadList = $upload->getuploadfileinfo ();


Import Picture Class
Import (' @.org.image ');

Add watermarks to m_ thumbnails, Image::water (' Original file path ', ' watermark Image address ')
Image::water ($uploadList [0][' Savepath ']. ' M_ '. $uploadList [0][' Savename '], App_path. ' Tpl/public/images/logo.png ');

Picture name assignment To 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 (' Upload picture Success! ');
}
Else
{
$this->error (' Upload picture Failed! ');
}
}
}
?>

Upload successfully generate two thumbnail images

What needs to be stated is:

thinkphp in the Picture upload Class (UploadFile.class.php) and Picture model class (Image.class.php), to the full version of the thinkphp package.

There is no need to create a folder in Lib (ORG), and then go to the official website to download the expansion pack to put the two files in the ORG folder.

Mine is the second case.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.