Code of three php multi-File Upload instances (1/3)

Source: Internet
Author: User

The code of the three php multi-File Upload instances often meets the File Upload requirement in php development and applications, and sometimes involves multi-file upload, next, let's take a look at the three php multi-File Upload instance codes that I provide. If you have a good time, let's take a look at these multi-File Upload functions.

Three php tutorials multi-File Upload instance code
In php development applications, we often encounter the File Upload requirement and sometimes need to upload multiple files. Let's take a look at the three php multi-File Upload instance codes that I provide, well, let's talk a little more about it. Let's see if these Multifile upload functions are suitable for you.

Sample Code:

<Form action = "" method = "post" enctype = "multipart/form-data">
<Input type = "file" name = "uploadfile []"> the name must be "[]".
<Input type = "file" name = "uploadfile []">

// Set the file types that can be uploaded by users.
$ Type = array ('gif', 'jpg ', 'png', 'zip', 'rar ');
$ Upload = new uploadfile ($ _ files ['uploadfile'], '/', 1024*1024, $ type );
Parameter description: 1: Form file; 2: Upload directory; 3: file size supported; 4: file type allowed
$ Icount = $ upload-> upload ();
If ($ icount> 0) {// upload successful
Print_r ($ upload-> getsaveinfo ());
*/

Class uploadfile {
Var $ postfile = array (); // File Uploaded by the user
Var $ custompath = ""; // customize the File Upload path
Var $ maxsize = ""; // Maximum File Size
Var $ lasterror = ""; // last error message
Var $ allowtype = array ('gif', 'jpg ', 'png', 'zip ', 'rar', 'txt ', 'Doc', 'pdf ');
Var $ endfilename = ""; // The final file name
Var $ saveinfo = array (); // Save the final information of the object
Var $ root_dir = ""; // location of the project on the hard disk

/**
* Constructor
* @ Access public
*/

Function uploadfile ($ arrfile, $ path = "_", $ size = 2097152, $ type = 0 ){
$ This-> postfile = $ arrfile;
$ This-> custompath = $ path = "_"? "": $ Path;
$ This-> maxsize = $ size;
If ($ type! = 0) $ this-> allowtype = $ arrfile;
$ This-> root_dir = $ _ server ['document _ root'];
$ This-> _ mkdir ($ this-> custompath );
}

/**
* Core code for File Upload
* @ Access public
* @ Return int number of successfully uploaded files
*/

Function upload (){
$ Ilen = sizeof ($ this-> postfile ['name']);
For ($ I = 0; $ I <$ ilen; $ I ++ ){
If ($ this-> postfile ['error'] [$ I] = 0) {// No error occurred during upload
// Obtain the current file name, temporary file name, size, and extension, which will be used later.
$ Sname = $ this-> postfile ['name'] [$ I];
$ Stname = $ this-> postfile ['tmp _ name'] [$ I];
$ Isize = $ this-> postfile ['SIZE'] [$ I];
$ Stype = $ this-> postfile ['type'] [$ I];
$ Sextn = $ this-> _ getextname ($ sname );

// Check whether the size of the currently uploaded file is valid.
If ($ this-> _ checksize ){
$ This-> lasterror = "the file you uploaded [". $ sname. "] exceeds the size supported by the system! ";
$ This-> _ showmsg ($ this-> lasterror );
Continue;
}

If (! Is_uploaded_file ($ stname )){
$ This-> lasterror = "your file is not uploaded through normal channels! ";
$ This-> _ showmsg ($ this-> lasterror );
Continue;
}
$ _ Filename = basename ($ sname, ".". $ sextn). "_". time (). ".". $ sextn;
$ This-> endfilename = $ this-> custompath. $ _ filename;

If (! Move_uploaded_file ($ stname, $ this-> root_dir. $ this-> endfilename )){
$ This-> lasterror = $ this-> postfile ['error'] [$ I];
$ This-> _ showmsg ($ this-> lasterror );
Continue;
}

// Store information about the current file for other programs to call.
$ This-> save_info [] = array ("name" => $ sname, "type" => $ sextn, "size" => $ isize, "path" => $ this-> endfilename );
}
}

Return sizeof ($ this-> save_info );
}

1 2 3

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.