Multi-File Upload class Code _php tutorial

Source: Internet
Author: User
Real support for single-file and multi-File upload class code, fixed $_files[$field[' name '] $field cannot use the variable can only and the form file name name= "userfile" consistent disadvantage $_ files['userfile' [' name '], hereThe filename in the file can be arbitrarily taken.

//index.htm
1. Single File Upload

2, multi-file upload

--------------------------------------------------------------------------------------------------------------- -----------------
//upload.php

Class file_upload{
Public $upload _path= './upload/';//The path of the uploaded file
Public $allow _type=array ();//file types allowed for uploading
Public $max _size= ' 20480 ';//maximum file size allowed
Public $overwrite Whether =false;//is set to overwrite mode
Public $renamed =false;//whether to use the name of the uploaded file directly or the system to automatically name

/**
* Private Variables
*/
Private $upload _file=array ();//save information for uploading successful files
Private $upload _file_num=0;//number of successful uploads
Private $SUCC _upload_file=array ();//successfully saved file information
/**
* Constructor
*
* @param string $upload _path
* @param string $allow _type
* @param string $max _size
*/
Public function __construct ($upload _path= './upload/', $allow _type= ' jpg|bmp|png|gif|jpeg ', $max _size= ' 204800 ')
{
$this->set_upload_path ($upload _path);
$this->set_allow_type ($allow _type);
$this->max_size= $max _size;
$this->get_upload_files ();
}
/**
* Set the upload path and determine
*
* @param string $path
*/
Public Function Set_upload_path ($path)
{
if (file_exists ($path)) {
if (is_writeable ($path)) {
$this->upload_path= $path;
}else{
if (@chmod ($path, ' 0666 '))
$this->upload_path= $path;
}
}else{
if (@mkdir ($path, ' 0666 ')) {
$this->upload_path= $path;
}
}
}
Set Upload file type
Public Function Set_allow_type ($types) {
$this->allow_type=explode ("|", $types);
}
Uploading files
Public Function Get_upload_files ()
{
foreach ($_files as $key = $field)
{
$this->get_upload_files_detial ($key);
}
}
Uploading the file data into the array
Public Function get_upload_files_detial ($field) {
if (Is_array ($_files["$field" [' Name ']))
{
for ($i =0; $i <>
{
if (0==$_files[$field [' Error '] [$i])
{
$this->upload_file[$this->upload_file_num][' name ']=$_files[$field [' name '] [$i];
$this->upload_file[$this->upload_file_num][' type ']=$_files[$field [' type '] [$i];
$this->upload_file[$this->upload_file_num][' size ']=$_files[$field [' Size '] [$i];
$this->upload_file[$this->upload_file_num][' tmp_name ']=$_files[$field] [' tmp_name '] [$i];
$this->upload_file[$this->upload_file_num][' error ']=$_files[$field [' Error '] [$i];
$this->upload_file_num++;
}
}
}
else {
if (0==$_files["$field" [' Error '])
{
$this->upload_file[$this->upload_file_num][' name ']=$_files[' $field ' [' name '];
$this->upload_file[$this->upload_file_num][' type ']=$_files["$field" [' type '];
$this->upload_file[$this->upload_file_num][' size ']=$_files["$field" [' Size '];
$this->upload_file[$this->upload_file_num][' tmp_name ']=$_files["$field" [' Tmp_name '];
$this->upload_file[$this->upload_file_num][' error ']=$_files[' $field ' [' Error '];
$this->upload_file_num++;
}
}
}
/**
* Check upload file is structure to meet specified conditions
*
*/
Public function Check ($i)
{
if (!empty ($this->upload_file[$i [' name '])) {
Check File size
if ($this->upload_file[$i] [' Size ']> $this->max_size*1024) $this->upload_file[$i] [' Error ']=2;
Set default server-side file name
$this->upload_file[$i] [' filename ']= $this->upload_path. $this->upload_file[$i] [' name '];
Get File path information
$file _info=pathinfo ($this->upload_file[$i] [' name ']);
Get file name extension
$file _ext= $file _info[' extension ';
Check file types
if (!in_array ($file _ext, $this->allow_type)) $this->upload_file[$i [' Error ']=5;
You need to rename the
if ($this->renamed) {
List ($usec, $sec) = Explode ("", Microtime ());
$this->upload_file[$i [' filename ']= $sec. substr ($usec, 2). $file _ext;
Unset ($USEC);
Unset ($SEC);
}
Check if the file exists
if (file_exists ($this->upload_file[$i [' filename '])) {
if ($this->overwrite) {
@unlink ($this->upload_file[$i [' filename ']);
}else{
$j = 0;
do{
$j + +;
$temp _file=str_replace ('. '). $file _ext, ' ($j. '). $file _ext, $this->upload_file[$i] [' filename ']);
}while (file_exists ($temp _file));
$this->upload_file[$i] [' filename ']= $temp _file;
unset ($temp _file);
Unset ($J);
}
}
Check complete.
} else $this->upload_file[$i] [' Error ']=6;
}
/**
* Upload Files
*
* @return True
*/
Public Function upload ()
{
$upload _msg= ';
for ($i =0; $i < $this->upload_file_num; $i + +)
{
if (!empty ($this->upload_file[$i [' name ']))
{
Check file
$this->check ($i);
if (0== $this->upload_file[$i [' Error '])
{
Uploading files
if (! @move_uploaded_file ($this->upload_file[$i [' Tmp_name '], $this->upload_file[$i] [' filename ']))
{
$upload _msg.= ' upload file '. $this->upload_file[$i] [' name ']. ' ERROR: ' $this->error ($this->upload_file[$i [' ERROR '] ).'!
';
}else
{
$this->succ_upload_file[]= $this->upload_file[$i] [' filename '];
$upload _msg.= ' upload file '. $this->upload_file[$i] [' name ']. ' Success
';
}
}else $upload _msg.= ' upload file '. $this->upload_file[$i [' name ']. ' ERROR: ' $this->error ($this->upload_file[$i] [' Error ']). '!
';
}
}
echo $upload _msg;
}
Error message
Public Function error ($ERROR)
{
Switch ($error) {
Case 1:
Return ' file size exceeds the value of upload_max_filesize option limit in php.ini ';
Break
Case 2:
Return ' file size exceeds the value specified by the Max_file_size option in the HTML form ';
Break
Case 3:
Return ' files are only partially uploaded ';
Break
Case 4:
Return ' No files are uploaded ';
Break
Case 5:
Return ' This file is not allowed to be uploaded ';
Break
Case 6:
Return ' file name is empty ';
Break
Default
Return ' ERROR ';
Break
}
}
Get successful data information for an array (alternate)
Public Function Get_succ_file () {
return $this->succ_upload_file;
}
}
$upload =new file_upload ('./upload/', ' jpg|bmp|png|gif|jpeg ');
$upload->upload ();
$t = $upload->get_succ_file ();
Print_r ($t);

?>


http://www.bkjia.com/PHPjc/445064.html www.bkjia.com true http://www.bkjia.com/PHPjc/445064.html techarticle real support for single-file and multi-File upload class code, fixed $_files[$field [' name '] $field cannot use the variable can only and the form file name Name= userfile consistent disadvantage $_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.