Thinkphp image upload code, simple thinphp image upload example

Source: Internet
Author: User
I recently found that it was very difficult to upload thinkphp images, and the code was forgotten. Later, I found a code example for uploading images on the Internet and performing paging operations for you to refer to. Recently, I found that it was very difficult to upload thinkphp images, and the code was forgotten. Later, I found a code example for uploading images and operating paging classes on the Internet for your reference.

If you think your project is not very good for testing code, you can create a new TestAction class.


1. execute the following code in TestAction. class. php of the controller.


//
Class TestAction extends
Action
{

Public function
Index ()
{

$ This-> display ();
}
Public function
Upload (){
If (! Empty ($ _ FILES )){
$ This-> _ upload ();
}
}
Public
Function
_ Upload (){
Import ("ORG. Net. UploadFile ");
$ Upload =
New
UploadFile ();
// Set the size of the uploaded file
$ Upload-> maxsize =
3145728;
// Set the Upload file type
$ Upload-> allowExts =
Explode (',', "jpg, gif, jpeg, png ");
// Set the nearby Upload Directory
$ Upload-> savePath =
"./Tpl/default/Public/image/"; // note that the directory is the relative path of the entry file


// You need to generate a thumbnail, which is only valid for image files.
// $ Upload-> thumb =
True;
// Set the path of the referenced image library package
// $ Upload-> imageClassPath =
'Org. Net. image ';
// Set the file suffix of the thumbnail to be generated
// $ Upload-> thumbPrefix
='M _, s _';
// Generate two thumbnails
// Set the maximum width of the thumbnail.
// $ Upload-> thumbMaxWidth
=
'000000 ′;
// Set the maximum height of the thumbnail.
// $ Upload-> thumbMaxHeight
= '000000 ′;
// Set file Upload rules


$ Upload-> saveRule =
Uniqid;
// Delete the source image
$ Upload-> thumbRemoveOrigin =
True;


If (! $ Upload-> upload ()){
// Capture Upload exceptions
$ This-> error ($ upload-> getErrorMsg ());


} Else {
// Obtain the information of the uploaded file
$ Info =
$ Upload-> getUploadFileInfo ();
$ This->
Success ("uploaded successfully ");
}

}



}
?>

2. create an index.html template in the indexfolder of default. the code is as follows:






<Title> New Document
Content = "EditPlus">



Content = "">



Enctype = "multipart/form-data">











</Form>



</Html>

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.