Three PHP multi-file Upload instance code (1/3) _php Tutorial

Source: Internet
Author: User
Three PHP multi-file Upload instance code in the PHP development application often meet the need to upload files, and sometimes encounter to upload more files, the following we have to look at the three php file upload instance code I provide, good crap don't say more to see these multi-file upload function suitable for you.

Three PHP tutorial multi-File Upload instance code
In the PHP development application often encounter file upload this need, and sometimes will encounter to upload more files, below we come to see I provide three PHP multi-file Upload instance code, good crap don't say more to see these multi-file upload function suitable for you.

Example code:


The name must be "[]"

Sets the types of files that are allowed to be uploaded by users.
$type = Array (' gif ', ' jpg ', ' png ', ' zip ', ' rar ');
$upload = new UploadFile ($_files[' uploadfile '), '/', 1024*1024, $type);
Parameter description: 1: File of the form, 2: Upload directory, 3: Support file Size, 4: Allow file type
$icount = $upload->upload ();
if ($icount > 0) {//upload successful
Print_r ($upload->getsaveinfo ());
*/

Class UploadFile {
var $postfile = array (); User-uploaded files
var $custompath = ""; Custom 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 last saved file name
var $saveinfo = array (); Save the final information for a file
var $root _dir = ""; The location of the project on the hard disk

/**
* Constructor function
* @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
* Number of successful files @return int upload
*/

Function upload () {
$ilen = sizeof ($this->postfile[' name '),
for ($i =0; $i < $ilen; $i + +) {
if ($this- >postfile[' ERROR ' [$i] = = 0) {//Upload no error occurred
//Take the current file name, temporary file name, size, 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);

//detects whether the current upload file size is legitimate.
if ($this->_checksize) {
$this->lasterror = "The file you uploaded [". $sname. "], over the system support size!";
$this->_showmsg ($this->lasterror);
Continue;
}

if (!is_uploaded_file ($stname)) {
$this->lasterror = "Your files are not uploaded by normal means!";
$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
}

Stores information about the current file so it can be called by other programs.
$this->save_info[] = Array ("name" = = $sname, "type" = = $sextn, "size" = = $isize, "path" = = $this->endfil ENAME);
}
}

return sizeof ($this->save_info);
}

1 2 3

http://www.bkjia.com/PHPjc/444829.html www.bkjia.com true http://www.bkjia.com/PHPjc/444829.html techarticle three PHP multi-file Upload instance code in the PHP development application often encounter file upload this need, and sometimes will encounter to upload more files, below we come to see I provide more than three PHP ...

  • 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.