File upload-Multiple files

Source: Internet
Author: User
Tags html form

<?php
Class FileUpload {
Private $filepath; Specify the path where the upload file is saved
Private $allowtype =array (' gif ', ' jpg ', ' png ', ' jpeg '); The type of file that is allowed to upload
Private $maxsize = 1000000; Maximum length of uploaded file 1M
Private $israndname =true; Whether to randomly rename, true false not random, use the original file name
Private $originName; Source file name
Private $tmpFileName; Temporary file name
Private $fileType; File type
Private $fileSize; File size
Private $newFileName; New file name
Private $errorNum = 0; Error number
Private $errorMess = ""; Used to provide error reporting
Used to initially enable uploading of files
1. Specify the upload path, 2, allowable type, 3, limit size, 4, whether to use random file name
So that the user can not pass the parameters by location, the following parameter to the value does not have the first several parameters also provide values
function __construct ($options =array ()) {
foreach ($options as $key = = $val) {
$key =strtolower ($key);
See if the index of the array in the user parameter is the same as the member property name
if (!in_array ($key, Get_class_vars (Get_class ($this)))) {
Continue
}

$this->setoption ($key, $val);
}


}


Private Function GetError () {
$STR = "Error uploading file <font color= ' red ' >{$this->originname}</font>:";

Switch ($this->errornum) {
Case 4: $str. = "No files are uploaded"; Break
Case 3: $str. = "file is only partially uploaded"; Break
Case 2: $str. = "The uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form"; Break
Case 1: $str. = "The upload file exceeds the value of the upload_max_filesize option in php.ini"; Break
Case-1: $str. = "type of final charge"; Break
Case-2: $str. = "File too large, upload file cannot exceed {$this->maxsize} bytes"; Break
Case-3: $str. = "Upload Failed"; Break
Case-4: $str. = "Failed to set up the directory to upload files, please re-specify the upload directory"; Break
Case-5: $str. = "The path of the uploaded file must be specified"; Break

Default: $str. = "Last known error";
}

return $str. ' <br> ';
}

Used to check file upload path
Private Function Checkfilepath () {
if (Empty ($this->filepath)) {
$this->setoption (' ErrorNum ',-5);
return false;
}

if (!file_exists ($this->filepath) | |!is_writable ($this->filepath)) {
if ([email protected] ($this->filepath, 0755)) {
$this->setoption (' ErrorNum ',-4);
return false;
}
}
return true;
}
To check the size of the file upload
Private Function Checkfilesize () {
if ($this->filesize > $this->maxsize) {
$this->setoption (' ErrorNum ', '-2 ');
return false;
}else{
return true;
}
}

Used to check file upload type
Private Function Checkfiletype () {
if (In_array (Strtolower ($this->filetype), $this->allowtype)) {
return true;
}else{
$this->setoption (' ErrorNum ',-1);
return false;
}
}
Set the file name after uploading
Private Function Setnewfilename () {
if ($this->israndname) {
$this->setoption (' NewFileName ', $this->prorandname ());
} else {
$this->setoption (' NewFileName ', $this->originname);
}
}

Set Random file names
Private Function Prorandname () {
$fileName =date ("Ymdhis"). Rand (100,999);
Return $fileName. '. '. $this->filetype;
}

Private Function SetOption ($key, $val) {
$this $key = $val;
}
Used to upload a file
function UploadFile ($fileField) {
$return =true;
Check File Upload path
if (! $this->checkfilepath ()) {
$this->errormess= $this->geterror ();
return false;
}


$name =$_files[$fileField [' name '];
$tmp _name=$_files[$fileField [' Tmp_name '];
$size =$_files[$fileField [' size '];
$error =$_files[$fileField] [' ERROR '];

if (Is_array ($name)) {
$errors =array ();

for ($i =0; $i <count ($name); $i + +) {
if ($this->setfiles ($name [$i], $tmp _name[$i], $size [$i], $error [$i]) {
if (! $this->checkfilesize () | |! $this->checkfiletype ()) {
$errors []= $this->geterror ();
$return =false;
}
}else{
$error []= $this->geterror ();
$return =false;
}

if (! $return)
$this->setfiles ();
}

if ($return) {
$fileNames =array ();

for ($i =0; $i <count ($name); $i + +) {
if ($this->setfiles ($name [$i], $tmp _name[$i], $size [$i], $error [$i]) {
$this->setnewfilename ();
if (! $this->copyfile ()) {
$errors = $this->geterror ();
$return =false;
}else{
$fileNames []= $this->newfilename;
}
}
}

$this->newfilename= $fileNames;
}

$this->errormess= $errors;
return $return;
} else {

if ($this->setfiles ($name, $tmp _name, $size, $error)) {
if ($this->checkfilesize () && $this->checkfiletype ()) {
$this->setnewfilename ();

if ($this->copyfile ()) {
return true;
}else{
$return =false;
}

}else{
$return =false;
}
}else{
$return =false;
}

if (! $return)
$this->errormess= $this->geterror ();


return $return;
}
}

Private Function CopyFile () {
if (! $this->errornum) {
$filepath =rtrim ($this->filepath, '/'). ' /‘;
$filepath. = $this->newfilename;

if (@move_uploaded_file ($this->tmpfilename, $filepath)) {
return true;
}else{
$this->setoption (' ErrorNum ',-3);
return false;
}

}else{
return false;
}
}

Setting up and $_files related content
Private Function setfiles ($name = "", $tmp _name= ", $size =0, $error =0) {

$this->setoption (' ErrorNum ', $error);

if ($error) {
return false;
}

$this->setoption (' Originname ', $name);
$this->setoption (' Tmpfilename ', $tmp _name);
$arrStr =explode ('. ', $name);
$this->setoption (' FileType ', Strtolower ($arrStr [Count ($ARRSTR)-1]));
$this->setoption (' fileSize ', $size);

return true;
}

Used to get the file name after uploading
function Getnewfilename () {
return $this->newfilename;
}
If the upload fails, call this method to view the error report
function Geterrormsg () {
return $this->errormess;
}
}

File upload-Multiple files

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.