Just write a file next to the class. Welcome Commentary

Source: Internet
Author: User
Just wrote a file upload class. Welcome Commentary



<title>Uploading a file form</title>





Define (Upload_dir, ' d:\\appserv\\www\\test\\upload\\ ');
Define (Upload_web_dir, ' \\test\\upload\\ ');

Class upload_file_class{

var $input _name;//Upload the name of file
var $max _size;//max upload size unit kb
var $allow the allowed extensions for _ext;//
var $err _info; Error message

/**
* Constructor function
*
* @param $input _name
* @param $max _size
* @param $allow _ext
* @return
*/
function __construct ($input _name, $max _size=1024, $allow _ext=array (' jpg ', ' png ', ' gif ')) {

$this->input_name = $input _name;
$this->max_size = $max _size;
$this->allow_ext = $allow _ext;
$this->err_info_arr = Array (

1=> ' upload file size exceeds ini set value ',
2=> ' upload file size exceeds the maximum value set by the form ',
3=> ' files are only partially uploaded ',
4=> ' No files were uploaded ',
5=> ' upload file size exceeds the allowed set value ',
6=> ' is not an allowed upload file type ',
7=> ' failed to create directory, please verify that you have permission to create directory ',
8=> ' failed to create the file, please try again. ',
9=> ' File Upload error occurred, please try again. '

);

}

/**
* File Upload
*
* @return $file _path_arr Upload a successful file path array
*/
Public function upload () {

Global $_files;
if (!is_array ($this->input_name)) $this->input_name = Array ($this->input_name);
$file _path_arr = Array ();
foreach ($this->input_name as $k = = $input _name) {

$__f = $_files[$input _name];
if (!empty ($__f)) {

if ($__f[' error '] > 0) {

Client uploads generate errors
$this->set_err ($__f[' error ');

}
else{

$__f[' size ' = $__f[' size ']/1024;
if ($this->max_size >= $__f[' size ') {

if ($this->check_ext ($__f[' name ')) {

$save _dir_info = $this->check_dir ();
if ($save _dir_info) {

$rand _num = Date (' Ymdhis '). Rand (0,10000000);
$ext = $this->get_img_ext ($__f[' name ');
$new _file_name = $rand _num. ". {$ext} ";
$full _path = $save _dir_info[' Full_dir '). $new _file_name;
$full _web_path = $save _dir_info[' Full_web_dir '). $new _file_name;
if (!file_exists ($full _path)) {

if (Move_uploaded_file ($__f[' tmp_name '), $full _path)) {
  • 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.