An example _php example of a file upload method for encapsulation thinkphp

Source: Internet
Author: User

The example of this article tells the package thinkphp a file upload method, share for everyone for reference. Specifically as follows:

Over the wblog, the upload function has been optimized. After the improved upload function can achieve thumbnail upload, image upload, generate watermark, file upload, and can upload the attachment in the background size (file size), type (file type), watermark (for the picture) to set. In addition, the uploaded files can be classified and saved to the appropriate folder for easy management.

The following is a simplified description of the improved upload function.

Thumbnail upload

The previous thumbnail upload feature uses the Kindeditor upload component, unless you upload the image before the thumbnail processing, or upload the original, the improved upload function can be set through the system to the size of the thumbnail, no matter how large you upload the original image, will generate the size of the picture set, and automatically delete the original image, effectively reduce the accumulation of spam information. Uploaded thumbnails are automatically saved to the Thumb folder

Picture upload

Upload pictures, you can set the size of the picture, upload the image type and add watermark.

File Upload

such as compressed files, document files, etc., can be added as required to upload file type and size.

Add watermark

Add a watermark to the uploaded picture.

The above upload function I encapsulated it in the model class AttachModel.class.php, in two ways to handle:

Copy Code code as follows:

/**
* Attachment upload: Upload pictures, thumbnails, files
* @param string $model upload module
* @param bool $type upload file type:: Image (picture); thumb (thumbnail); file (Files)
*/

Public function upload ($model = null, $type = ' image ') {
Import Upload Class
Import (' ORG.NET.UploadFile ');
$upload = new UploadFile ();
$upload->saverule = ' uniqid '; Set upload file rules
$upload->maxsize = C (' maxSize ') *1024*1024; Upload file size
$upload->allowexts = Explode (', ', C (' allowexts '));//File type

if ($model) {
$upload->savepath = './public/uploads/'. $model. ' /';
if (!file_exists ($upload->savepath)) {
mkdir ($upload->savepath);
}
}else{
$upload->savepath = './public/uploads/thumb/';
if (!file_exists ($upload->savepath)) {
mkdir ($upload->savepath);
}
}
if (In_array ($type, Array (' image ', ' thumb ')) {
$upload->thumb = true;
$upload->thumbremoveorigin = true;////delete artwork
$upload->thumbprefix = ' wb_ ';
$upload->thumbmaxwidth = $type = = ' thumb '? C (' Thumb_w '): C (' Image_w ');//thumbnail width
$upload->thumbmaxheight = $type = = ' thumb '? C (' Thumb_h '): C (' Image_h ');//thumbnail height

}
if (! $upload->upload ()) {
return $upload->geterrormsg ();
}else{
$uploadlist = $upload->getuploadfileinfo ();
if (C (' Iswater ') && $type = = ' image ') {
Import (' ORG. Util.image ');
Add a watermark to the m_ thumbnail, image::water (' original filename ', ' watermark Image address ')
foreach ($uploadlist as $key => $value) {
Image::water ($value [' Savepath ']. ' Wb_ '. $value [' Savename '], './public/admin/images/water.png ');
}
}
}
if (In_array ($type, Array (' Image ', ' file ')) {
return $this->_add ($uploadlist, $model);
}else{
return $uploadlist [0][' savename '];//returns thumbnail save name
}
}
/*
* Upload the attachment into the attach required data, stored in the table and return the array
* */
Private Function _add ($uploadlist, $module = ') {
$j = count ($uploadlist);
$v = Array ();
foreach ($uploadlist as $key => $value) {

$v [$key] [' name '] = $value [' name '];
$v [$key] [' savename '] = $value [' Savename '];
$v [$key] [' savepath '] = substr ($value [' Savepath '], 2);
$v [$key] [' size '] = $value [' Size '];
$v [$key] [' userId '] = $_session[c (' User_auth_key ')];
$v [$key] [' uploadtime '] = time ();
$v [$key] [' alt '] = $_post[' alt '] [$key];
$v [$key] [' module '] = $_post[' module '];//current project path
$v [$key] [' recordid '] = $_post[' RecordID '];//current project path
$this->add ($v [$key]);
if ($this->thumb) {

$v [$key] [' prefix '] = $this->thumbprefix;
}
$v [$key] [' id '] = M (' Attach ')->getlastinsid ();
}
return $v;
}

I hope this article will be helpful to everyone's thinkphp framework program design.

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.