Home php教程 php手册 转载-ThinkPHP文件上传类的分享

转载-ThinkPHP文件上传类的分享

Jun 13, 2016 am 11:26 AM
org upload use share document of kind

上传类使用ORG类库包中的Net.UpdateFile类,最新版本的上传类包含的功能如下(有些功能需要结合ThinkPHP系统其他类库):

1.基本上传功能

2.支持批量上传

3.支持生成图片缩略图

4. 自定义参数上传

5.上传检测(包括大小、后缀和类型)

6.支持上传类型、附件大小、上传路径定义

7.支持哈希或者日期子目录保存上传文件

8.上传图片的安全性检测

9.支持上传文件命名规则

10. 支持对上传文件的Hash验证


在ThinkPHP中使用上传功能无需进行特别处理。例如,下面是一个带有附件上传的表单提交:

 代码如下 复制代码






 

注意表单的Form标签中一定要添加 enctype=”multipart/form-data” 文件才能上传。因为表单提交到当前模块的upload操作方法,所以我们在模块类里面添加下面的upload方法即可:

 代码如下 复制代码
Public function upload(){
import(“ORG.Net.UploadFile”);
$upload = new UploadFile(); // 实例化上传类
$upload->maxSize = 3145728 ; // 设置附件上传大小
$upload->allowExts = array(‘jpg’, ‘gif’, ‘png’, ‘jpeg’); // 设置附件上传类型
$upload->savePath = ‘./Public/Uploads/’; // 设置附件上传目录
if(!$upload->upload()) { // 上传错误 提示错误信息
$this->error($upload->getErrorMsg());
}else{ // 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
// 保存表单数据 包括附件数据
$User = M(“User”); // 实例化User对象
$User->create(); // 创建数据对象
$User->photo = $info[0]["savename"]; // 保存上传的照片 根据需要自行组装
$User->add(); // 写入用户数据到数据库
$this->success(“数据保存成功!”);
}

 

首先是实例化上传类

 代码如下 复制代码
import(“ORG.Net.UploadFile”);
$upload = new UploadFile(); // 实例化上传类

 

实例化上传类之后,就可以设置一些上传的属性(参数),支持的属性有:

maxSize: 文件上传的最大文件大小(以字节为单位)默认为-1 不限大小

savePath:文件保存路径,如果留空会取UPLOAD_PATH常量定义的路径

saveRule:上传文件的保存规则,必须是一个无需任何参数的函数名,例如可以是 time、 uniqid com_create_guid 等,但必须能保证生成的文件名是唯一的,默认是uniqid

hashType:上传文件的哈希验证方法,默认是md5_file

autoCheck:是否自动检测附件,默认为自动检测

uploadReplace:存在同名文件是否是覆盖

allowExts:允许上传的文件后缀(留空为不限制),使用数组设置,默认为空数组

allowTypes:允许上传的文件类型(留空为不限制),使用数组设置,默认为空数组

thumb:是否需要对图片文件进行缩略图处理,默认为false

thumbMaxWidth:缩略图的最大宽度,多个使用逗号分隔

thumbMaxHeight:缩略图的最大高度,多个使用逗号分隔

thumbPrefix:缩略图的文件前缀,默认为thumb_   (如果你设置了多个缩略图大小的话,请在此设置多个前缀 )

thumbSuffix:缩略图的文件后缀,默认为空   (如果你设置了多个缩略图大小的话,请在此设置多个后缀 )

thumbPath:缩略图的保存路径,留空的话取文件上传目录本身

thumbFile:指定缩略图的文件名

thumbRemoveOrigin:生成缩略图后是否删除原图

autoSub:是否使用子目录保存上传文件

subType:子目录创建方式,默认为hash,可以设置为hash或者date

dateFormat:子目录方式为date的时候指定日期格式

hashLevel:子目录保存的层次,默认为一层

以上属性都可以直接设置,例如:

 代码如下 复制代码
$upload->thumb = true
$upload->thumbMaxWidth = “50,200″
$upload->thumbMaxHeight = “50,200″

 

其中生成缩略图功能需要Image类的支持。

设置好上传的参数后,就可以调用 UploadFile类的upload方法进行附件上传,如果失败,返回false,并且用getErrorMsg方法获取错误提示信息;如果上传成功, 可以通过调用getUploadFileInfo方法获取成功上传的附件信息列表。因此getUploadFileInfo方法的返回值是一个数组,其中 的每个元素就是上传的附件信息。每个附件信息又是一个记录了下面信息的数组,包括:

key:附件上传的表单名称

savepath:上传文件的保存路径

name:上传文件的原始名称

savename:上传文件的保存名称

size:上传文件的大小

type:上传文件的MIME类型

extension:上传文件的后缀类型

hash:上传文件的哈希验证字符串

文件上传成功后,就可以通过这些附件信息来进行其他的数据存取操作,例如保存到当前数据表或者单独的附件数据表都可以。

如果需要使用多个文件上传,只需要修改表单,把

XML/HTML代码

 代码如下 复制代码

改为

XML/HTML代码

 代码如下 复制代码



或者

PHP代码


两种方式的多附件上传系统的文件上传类都可以自动识别。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to share Quark Netdisk to Baidu Netdisk? How to share Quark Netdisk to Baidu Netdisk? Mar 14, 2024 pm 04:40 PM

Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? Mar 14, 2024 pm 02:07 PM

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

What is hiberfil.sys file? Can hiberfil.sys be deleted? What is hiberfil.sys file? Can hiberfil.sys be deleted? Mar 15, 2024 am 09:49 AM

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

See all articles