Three PHP multi-file upload example codes (1/3)_PHP tutorial

WBOY
Release: 2016-07-20 11:08:53
Original
878 people have browsed it

Three PHP multi-file upload example codes. In PHP development applications, we often encounter the need to upload files. Sometimes we also encounter the need to upload multiple files. Let's take a look at the three PHP multi-file upload example codes I provided. Okay, without further ado, let’s see if these multiple file upload functions are suitable for you.

Three PHP tutorial multi-file upload example codes
In PHP development applications, we often encounter the need to upload files, and sometimes we also encounter the need to upload multiple files. Let’s take a look at the three methods I provided. PHP multi-file upload example code, without further ado, let’s see if these multi-file upload functions are suitable for you.

Sample code:


The name must be like this with "[]"

//Set what users are allowed to upload File type.
$type = array('gif', 'jpg', 'png', 'zip', 'rar');
$upload = new uploadfile($_files['uploadfile'], '/', 1024*1024, $type);
Parameter description: 1: form file, 2: upload directory, 3: supported file size, 4: allowed file type
$icount = $upload->upload() ;
if($icount > 0) { //Upload successful
print_r($upload->getsaveinfo());
*/

class uploadfile {
var $postfile = array(); // Files uploaded by users
var $custompath = ""; // Customized file upload path
var $maxsize = ""; lasterror = ""; // Last error message
var $allowtype = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf ');
var $endfilename = ""; // The final saved file name
var $saveinfo = array(); // The final information of the saved file
var $root_dir = ""; // The 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
//Get 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 currently uploaded file size is legal.
if($this->_checksize){
$this->lasterror = "The file you uploaded [".$sname."] exceeds the system supported size!";
$this- >_showmsg($this->lasterror);
continue;
}

if(!is_uploaded_file($stname)) {
$this->lasterror = "Your file was 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;
}

//Storage information about the current file so that it can be called by other programs.
$this->save_info[] = array("name" => $sname, "type" => $sextn, "size" => $isize, "path" => $this- >endfilename);
}
}

return sizeof($this->save_info);
}

1 2 3

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444829.htmlTechArticleThree PHP multi-file upload example codes In PHP development applications, file upload is often encountered, and sometimes You may encounter the need to upload multiple files. Let’s take a look at the three PHP models I provide...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template