File upload class, to achieve file upload function

Source: Internet
Author: User
Tags html form http post php file upload

/**
*==================================================================
* upload.class.php file Upload class, to achieve file upload function
* March 27, 2013 0:37:15
*==================================================================
*/

Class upload{
Private $path; File upload Directory
Private $max _size; Upload File size limit
Private $errno; Error message number
Private $mime = Array (' Image/jpeg ', ' image/png ', ' image/gif ');//file type allowed for uploading

/**
* Constructor function,
* @access Public
* @param $path String upload path
*/
Public function __construct ($path = Upload_path) {
$this->path = $path;
$this->max_size = 1000000;
}

/**
* File Upload Method, sub-directory storage files
* @access Public
* @param $file array containing the uploaded file information
* @return Mixed successfully returned the uploaded file name, failed to return false
*/
Public function up ($file) {
Determine if a file is uploaded via HTTP POST to prevent malicious spoofing
/*
if (! is_uploaded_file ($file [' tmp_name '])) {
$this->errno = 5; Set the error message number to 5 to indicate an illegal upload
return false;
}
*/

Determine if the browser is successfully uploaded to the server side
if ($file [' error '] = = 0) {
# Upload to Temp folder successfully, processing temporary files
Upload type judgment
if (!in_array ($file [' type '], $this->mime)) {
# type Wrong
$this->errno =-1;
return false;
}

Determine file size
if ($file [' Size '] > $this->max_size) {
# size exceeds the upload limit in the configuration file
$this->errno =-2;
return false;
}

Get the directory where the uploaded files are stored
$sub _path = Date (' Ymd '). ' /‘;
if (!is_dir ($this->path. $sub _path)) {
# This directory does not exist and is created
mkdir ($this->path. $sub _path);
}

File Rename, by current date + random number + suffix name
$file _name = Date (' Ymdhis '). Uniqid (). STRRCHR ($file [' name '], '. ');

Ready to start uploading.
if (Move_uploaded_file ($file [' Tmp_name '], $this->path. $sub _path. $file _name)) {
# Mobile Success
Return $sub _path. $file _name;
} else {
# move failed
$this->errno =-3;
return false;
}

} else {
# failed to upload to Temp folder, set error number according to its error number
$this->errno = $file [' ERROR '];
return false;
}

}

/**
* Multi-File Upload method
* @access Public
* @param $file array containing the uploaded file information, is a two-dimensional array
* @return Array succeeded in returning the uploaded file name, if there is a failure, it is not very good to handle.
*/
Public Function Multiup ($files) {
In multi-file upload, upload file information is also a multidimensional array, such as $_files[' UserFile ' [' Name '][0],$_files[' userfile '] [' name '][1]
We just need to iterate through the array, get the information for each uploaded file, call the up method in turn
foreach ($files [' name '] as $key = + $value) {
# code ...
$file [' name '] = $files [' name '] [$key];
$file [' type '] = $files [' type '] [$key];
$file [' tmp_name '] = $files [' Tmp_name '] [$key];
$file [' error '] = $files [' ERROR '] [$key];
$file [' size '] = $files [' Size '] [$key];
Call the up method to complete the upload
$filename [] = $this->up ($file);
}
return $filename;
}

/**
* Get error information and get the appropriate error message based on the error number
* @access Public
* @return String returns an error message
*/
Public Function error () {
Switch ($this->errno) {
Case-1:
Return ' Please check your file type, currently supported types are '. Implode (', ', $this->mime);
Break
Case-2:
Return ' file exceeds the system-specified size, maximum cannot exceed '. $this->max_size;
Break
Case-3:
Return ' file move failed ';
Break
Case 1:
Return ' uploaded file exceeds the Upload_max_filesize option limit value in PHP.ini, and its size is '. Ini_get (' upload_max_filesize ');
Break
Case 2:
The return ' upload file size exceeds the value specified by the Max_file_size option in the HTML form, and its size is '. $_post[' max_file_size '];
Break
Case 3:
Return ' files are only partially uploaded ';
Break
Case 4:
Return ' No files are uploaded ';
Break
Case 5:
Return ' illegally uploaded ';
Break
Case 6:
Return ' Unable to find temp folder ';
Break
Case 7:
Return ' file failed to write to temp folder ';
Break
Default
Return ' unknown error, supernatural event ';
Break
}
}
}

/*
Test code: Single File Upload
<form method= ' POST ' action= ' upload.php ' enctype= ' Multipart/form-data ' >
<input type = ' hidden ' name= ' max_file_size ' value = ' 2000000 '/>
<input type = ' file ' name = ' picture '/>
<input type = ' submit ' value = ' upload '/>
</form>

$upload = new Upload;

if ($file _name = $upload->up ($_files[' picture ')) {
Echo ' upload successful, file name ', $file _name;
} else {
Echo ' upload failed with error message: ', $upload->error ();
}
*/

/*
Test code: Multi-File Upload
<form action= "test.php" method= "POST" enctype= "Multipart/form-data" >
<label for= "Upload image" ></label> <input type= "file" Name= ' logos[] '/> <br/>
<label for= "Upload image" ></label> <input type= "file" Name= ' logos[] '/> <br/>
<label for= "Upload image" ></label> <input type= "file" Name= ' logos[] '/> <br/>
<input type= "Submit" name= ' OK '/>
</form>
$upload = new Upload ();
$filename = $upload->multiup ($_files[' logos ');
Echo ' <pre> ';
Var_dump ($filename);
Echo ' <pre> ';
*/

File upload class, to achieve file upload function

Related Article

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.