Php file Upload class

Source: Internet
Author: User
Tags foreach chr file upload ord php file upload
The code is as follows: Copy code

 

<? Php

Class Uploader
{
Var $ _ base_dir = null;
Var $ _ rel_dir = null;
Var $ _ random_fname = false;
Var $ _ random_fname_len = 5;
Var $ _ fname_filter = null;
Var $ _ ftype_filter = null;
Var $ _ origin_paths = array ();

Function Uploader ($ base_dir, $ rel_dir)
    {
$ This-> _ base_dir = $ base_dir;
$ This-> _ rel_dir = $ rel_dir;
    }
Function setRandomFileName ($ random_fname, $ random_fname_len = 5)
    {
$ This-> _ random_fname = $ random_fname;
$ This-> _ random_fname_len = $ random_fname_len;
    }
Function setFileTypeFilter ($ filter)
    {
$ This-> _ ftype_filter = $ filter;
    }
Function addFile ($ file, $ origin_path = '')
    {
$ File = trim ($ file );
$ Origin_path = trim ($ origin_path );
If (array_key_exists ($ file, $ this-> _ origin_paths ))
Return;
$ This-> _ origin_paths [$ file] = $ origin_path;
    }
Function upload ()
    {
Foreach ($ this-> _ origin_paths as $ file => $ origin_path)
        {
$ Result = $ this-> _ uploadFile ($ file, $ origin_path );

If ($ result! = 'Success ')
Return $ result;
        }
Return 'success ';
    }
/*
* @ Desc Upload an attachment
* @ Return Success failure return failure type
* @ Param $ file Name $ orgin_path file path
*/
Function _ uploadFile ($ file, $ origin_path) // upload an attachment
    {
$ Ffile = $ _ FILES [$ file] ['tmp _ name']; // temporary file name stored on the server after the file is uploaded.
$ Fname = $ _ FILES [$ file] ['name']; // The original name of the client machine file.
$ Fsize = $ _ FILES [$ file] ['size']; // size of the uploaded file
$ Ftype = $ _ FILES [$ file] ['type']; // MIME type of the file
$ New_path = '';
       
If (! Empty ($ fname) & is_uploaded_file ($ ffile ))
        {
If (! Empty ($ this-> _ ftype_filter )&&! Is_null ($ this-> _ ftype_filter ))
            {
$ Match = false;
$ Extensions = explode (',', $ this-> _ ftype_filter );
Foreach ($ extensions as $ extension)
                {
If (strtolower (strrchr ($ fname, '.') = '.'. strtolower (trim ($ extension )))
                    {
$ Match = true;
Break;
                    }
                }
If (! $ Match)
Return 'errorfiletypefilternotmatch ';
            }
$ Fpath = $ this-> _ base_dir. $ this-> _ rel_dir;
If ($ this-> _ random_fname)
$ Fname = $ this-> _ getUniqueFileName ($ fname, $ this-> _ random_fname_len );
Copy ($ ffile, $ fpath. $ fname) or die ('upload failed! ');
$ New_path = $ this-> _ rel_dir. $ fname;
        }
If (! Empty ($ origin_path )&&! Empty ($ new_path) & $ origin_path! = $ New_path)
        {
$ This-> delete ($ origin_path );
        }
If (! Empty ($ new_path ))
$ This-> _ origin_paths [$ file] = $ new_path;
$ Erroe = $ _ FILES [$ file] ['error'];
Switch ($ Erroe ){
Case 1:
Return 'errexceeduploadmaxfilesize ';
Break;
Case 2:
Return 'errexceedhtmlmaxfilesize ';
Break;
Case 3:
Return 'errpartfiletrans ';
Break;
// Case 4:
// Return 'errnofiletrans ';
// Break;
Default:
Return 'success ';
        }     
    }
/*
* @ Desc get path
* @ Return path
* @ Param none
*/
Function getFilePath ()
    {
Return $ this-> _ origin_paths;
    }
Function getFileAbsPath ()
    {
$ Paths = array ();
Foreach ($ this-> _ origin_paths as $ path)
        {
$ Paths [] = $ this-> _ base_dir. $ path;
        }
Return $ paths;
    }
Function delete ($ fpath)
    {
If (! Empty ($ fpath) & is_file ($ this-> _ base_dir. $ fpath ))
Unlink ($ this-> _ base_dir. $ fpath) or die ('unlink error ');
    }
Function _ getUniqueFileName ($ fname, $ len)
    {
$ Timestamp = date ('ymdhis ');
Srand (double) microtime () * 1000000 );
For ($ I = 0, $ randfname = ''; $ I <$ len; $ I ++)
        {
$ Num = rand (0, 35 );
If ($ num <10)
$ Randfname. = chr (ord ('0') + $ num );
Else
$ Randfname. = chr (ord ('A') + $ num-10 );
        }
Return $ timestamp. '_'. $ randfname. strtolower (strrchr ($ fname ,'.'));
    }
}
?>

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.