thinkphp File Upload Example tutorial, _php Tutorial

Source: Internet
Author: User

thinkphp File Upload Example tutorial,


File upload is a common feature in many PHP program projects, today this article to share a complete example to implement the Thinkphp file upload function. Here's how:

First, action part:

The FileAction.class.php page code is as follows:

<?phpclass Fileaction extends action{function index () {$file =m (' file ');    $list = $file->select ();    $this->assign (' filelist ', $list);    $this->display ();      } function upload () {//File upload address submitted to him, and after the upload is complete, return a message to write to the database if (empty ($_files)) {$this->error (' must choose to upload files ');      }else{$a = $this->up ();          if (Isset ($a)) {//write a custom C method to the database if ($this->c ($a)) {$this->success (' upload succeeded ');          } else{$this->error (' Write database failed ');        }}else{$this-error (' Upload file exception, contact your system administrator ');    }}} Private Function C ($data) {$file =m (' file ');    $num = ' 0 ';            for ($i = 0; $i < count ($data)-1; $i + +) {$data [' filename ']= $data [$i] [' savename '];      if ($file->data ($data)->add ()) {$num + +;      }} if ($num ==count ($data)-1) {return true;    }else {return false; }} Private function up () {//complete the call to thinkphp related, File Upload class import (' @.org.uploadfile ');/upload class UploadFile.class.php Copy to lib/org folder $upload =new uploadfile (); $upload->maxsize= ' 1000000 ';//default is-1, no limit on upload size $upload->savepath= './public/upload/';// Save the path suggest a directory with the main file or a sub-directory to save $upload->saverule=uniqid;//upload file filename save rule $upload->uploadreplace=true;// If there is a file with the same name overwrite $upload->allowexts=array (' jpg ', ' jpeg ', ' png ', ' gif '),//The file type that is allowed to upload $upload->allowtypes=array (' Image/png ', ' image/jpg ', ' image/jpeg ', ' image/gif ');//detect MIME type $upload->thumb=true;//open picture file thumbnail $upload    Thumbmaxwidth= ' 300,500 ';    $upload->thumbmaxheight= ' 200,400 '; $upload->thumbprefix= ' s_,m_ ';//thumbnail file prefix $upload->thumbremoveorigin=1;//If a thumbnail is generated, delete the original if ($upload->upload      ()) {$info = $upload->getuploadfileinfo ();    return $info; }else{$this->error ($upload->geterrormsg ())///specifically used to get uploaded error messages}}}?>

Second, the View Template section:

The template file index.html code is as follows:

 
  
   
   Small Picture:
  
   
Large

It is believed that the example mentioned in this paper can be used as a reference for the development of thinkphp program.




http://www.bkjia.com/PHPjc/868241.html www.bkjia.com true http://www.bkjia.com/PHPjc/868241.html techarticle thinkphp File Upload instance tutorial, file upload is a common feature in many PHP program projects, today this article to share a complete example, to achieve the thinkphp file upload ...

  • Related Article

    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.