thinkphp implementation file upload simultaneously save file path in database

Source: Internet
Author: User
Tags file upload hash php and save file string format


thinkphp has file upload class we just need to call it. No other operations are required, let's briefly introduce this class.

Upload class using the ORG.Net.UpdateFile class, the latest version of the upload class contains the following features (some features need to be combined with the thinkphp system other class library):

Basically transfer function
Support Bulk Upload
Support for creating thumbnail images of pictures
Custom parameter Upload
Upload detection (including size, suffix, and type)
Support Overlay Mode upload
Support upload type, attachment size, upload path definition
Support hash or date subdirectory save upload file
Security detection of uploaded images
Support for uploading file naming rules
Support for hash verification of uploaded files

Using the upload feature in thinkphp eliminates the need for special handling

Upload a single file, this article to upload pictures for example, upload effect as shown in the picture

Create the database upload_img to save the upload path

The code is as follows Copy Code

CREATE TABLE ' seminar_upload_img ' (
' id ' int (one) not NULL auto_increment,
' Img_name ' varchar (255) DEFAULT NULL COMMENT ' picture name ',
' Img_url ' varchar (255) DEFAULT NULL COMMENT ' picture path ',
' Create_time ' text,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=23 DEFAULT Charset=utf8;

Connect the database in the public profile common/conf.php and configure the address

  code is as follows copy code
Return Array (
' Db_type ' => ' MySQL ',
' Db_host ' => ' 127.0.0.1 ',
' Db_name ' => ' seminar ',
' Db_user ' => ' root ',
' Db_pwd ' => ' root ',
' Db_port ' =>3306,
' Db_prefix ' => ' seminar_ ',
' Db_charset ' => ' UTF8 ',

' Show_page_trace ' =>true,

/* Address Replacement * *
' Tmpl_parse_string ' =>array (
' __upload__ ' =>__root__. ' /public/uploads ',
),
);


View File upload/index.html

The code is as follows Copy Code
<! DOCTYPE html>
<title></title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<style type= "Text/css" >
#img {height:22px border: #000 2px solid}
#button {height:30px; width:100px;}
</style>
<body>
<div><notemply name= "Data" >
"
</notemply></div>
<div class= "result" > upload allowed file types: ' jpg ', ' gif ', ' PNG ', ' JPEG ' image file </div><br>
<form action= "{: U (' Upload/upload ')}" method= "Post" enctype= "Multipart/form-data" >
<input type= "file" name= "image"/>
<input type= "Submit" value= "Upload" id= "button" >
</form>
</body>


Implement upload file in controller UploadController.class.php

The code is as follows Copy Code
namespace Home\controller;
Use Think\controller;
Class Uploadcontroller extends Controller {
Public Function index () {
$img =m (' upload_img ');
$sel = $img->order (' create_time desc ')->find ();
$this->assign (' data ', $sel);
$this->display ();
}
Public function upload () {
$upload _img=m (' upload_img ');
if (!empty ($_files)) {
Upload a single image
$upload = new \think\upload ()///materialized upload class
$upload->maxsize = 1*1024*1024//Set attachment upload size
$upload->exts = array (' jpg ', ' gif ', ' PNG ', ' jpeg ');//Set attachment upload type
$upload->rootpath = ' public/uploads/'; Set attachment upload root directory
$upload->savepath = '; Set attachment upload (sub) directory
$upload->savename=array (' uniqid ', ');//upload file Save rule
$upload->autosub = true;//automatically use subdirectories to save uploaded files
$upload->subname = Array (' Date ', ' Ymd ');
Upload a single picture
$info = $upload->uploadone ($_files[' image '));
if (! $info) {//Upload error message
$this->error ($upload->geterror ());
}else{//upload successfully upload file information
$img _url= $info [' Savepath ']. $info [' Savename '];
$data [' Img_url ']= $img _url;
$data [' Img_name ']= $info [' Savename '];
$data [' Create_time ']=now_time;
$upload _img->create ($data);
$result = $upload _img->add ();
if (! $result) {
$this->error (' Upload failed! ');
}else{
$this->success (' upload success ');
}
}
}
}
}

thinkphp to achieve multiple file upload

The code is as follows Copy Code

<?php

Class Uploadaction extends Action {
function upload () {
$file =m (' upload_img ');
$list = $file->select ();
$this->assign (' list ', $list);
$this->display ();
}
function uploads () {
File upload address submitted to him, and upload completed after the return of a message to write to the database
If $_files is empty, I'll let the action give us an error indicating that the user must choose to upload the file, then call the Up method
if (empty ($_files)) {
$this->error (' must choose to upload file ');
}
else{
$result = $this->up ();
if (Isset ($result)) {
Ways to write to a database
if ($this->c ($result)) {
$this->success (' Upload success! ');
}else{$this->error (' Write database failed! ');}

}else{$this->error (' upload file has an exception, please contact the system Administrator ');}
}
}
The core method of uploading files
Private function up () {
Private method, in this method, completes the call of the file upload class related to the thinkphp
Import (' @.org.uploadfile ');
$upload =new UploadFile ();
$upload->maxsize= ' 10000000 '//refers to the size of the uploaded file, the default is-1 is an infinite size
$upload->savepath= './public/upload/'; Where to save files after uploading? It is best to use the statistical catalogue
$upload->saverule= ' uniqid '; File name save rule for uploading files
$upload->autocheck=true; Whether attachments are automatically detected
$upload->uploadreplace=true;//If a file with the same name is overwritten
$upload->allowexts=array (' jpg ', ' jpeg ', ' gif ', ' PNG ');//allow the extension of the surviving file
$upload->allowtypes=array (' image/png ', ' image/jpg ', ' image/png ', ' image/jpeg '); Detecting MIME Types
$upload->thumb=true;//whether to open picture file abbreviations
$upload->thumbmaxwidth= ' 200,300 '; A string format to pass, if you want to have more than one, that is used here, split write multiple parameters
$upload->thumbmaxheight= ' 400,500 ';
$upload->thumbprefix= ' s_,m_ '; Thumbnail file prefix
$upload->thumbsuffix= '-s,-m ';/suffix
$file->thumbremoveorigin=true;
There is also a file name in the database
$upload->thumbremoveorigin=1;//If a thumbnail is generated, delete the original image
Upload () failed to return true if upload succeeded false
if ($upload->upload ()) {
$info = $upload->getuploadfileinfo ()//local variable, save upload success Information
return $info;
}else{
$this->error ($upload->geterrormsg ());
}

}
Private Function C ($data) {
$file =m (' upload_img ');
if ($data) {
for ($i =0; $i <count ($data); $i + +) {
$img [' img ']= $data [$i] [' savename ']; Equivalent to $img=array (' img ' => $data [0][' Savename ']);
$file->add ($IMG);
}
return true;
}
}
}
?>

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.