File upload class

Source: Internet
Author: User

Class yl_upload_class
{
#*************************************** ******************
# Create a directory function
#*************************************** ******************
Function createfolder ($ yl_path)
{
If (! File_exists ($ yl_path ))
   {
$ This-> createfolder (dirname ($ yl_path ));
@ Mkdir ($ yl_path, 0777 );
   }
Return $ this-> createfolder;
}
#*************************************** ******************
# Get the file name, size, type, and temporary file name
#*************************************** ******************
Function yl_getfilename ($ yl_type)
{
Global $ yl_filedata, $ yl_directroy, $ yl_settingsnew;
Global $ yl_maxsize, $ yl_sizeformat, $ yl_arrext, $ yl_ext;
Return $ _ FILES [$ yl_filedata] [$ yl_type];
}
#*************************************** ******************
# Getting the file size
#*************************************** ******************
Function yl_getfilesize ()
{
Global $ yl_filedata, $ yl_directroy, $ yl_settingsnew;
Global $ yl_maxsize, $ yl_sizeformat, $ yl_arrext, $ yl_ext;
If ($ this-> yl_getfilename ('size') = 0 ){
$ This-> alert ("Select Upload file! ");
Exit;
   }
If ($ this-> yl_getfilename ('size')> $ yl_maxsize ){
$ Yl_maxsizek = $ yl_maxsize/1024;
$ This-> alert ("the file to be uploaded exceeds the limit $ yl_maxsizek.K! ");
Exit;
   }
Switch (strtolower ($ yl_sizeformat )){
Case 'B ':
Return $ this-> yl_getfilename ('size'). 'B ';
Break;
Case 'K ':
Return ($ this-> yl_getfilename ('size')/1024). 'K ';
Break;
Case'm ':
Return ($ this-> yl_getfilename ('size')/(1024*1024). 'M ';
   }
}
#*************************************** ******************
# Obtain the file extension
#*************************************** ******************
Function yl_getfiletype ()
{
Global $ yl_filedata, $ yl_directroy, $ yl_settingsnew;
Global $ yl_maxsize, $ yl_sizeformat, $ yl_arrext, $ yl_ext;
$ Yl_temp_arr = explode (".", $ this-> yl_getfilename ('name '));
$ Yl_file_ext = array_pop ($ yl_temp_arr );
$ Yl_file_ext = trim ($ yl_file_ext );
$ Yl_file_ext = strtolower ($ yl_file_ext );
// Check the extension
If (in_array ($ yl_file_ext, $ yl_arrext) === false ){
$ This-> alert ("the Upload file type is limited! ");
Exit;
     }
Return $ yl_file_ext;
}
#*************************************** ******************
# Upload
#*************************************** ******************
Function yl_uploadfile ()
{
Global $ yl_filedata, $ yl_directroy, $ file_urldirectroy, $ yl_settingsnew;
Global $ yl_maxsize, $ yl_sizeformat, $ yl_arrext, $ yl_ext;
$ Yl_file_nameold = $ this-> yl_getfilename ('name'); // original file name
$ Yl_file_namenews = date ('ymmd'). '_'. md5 (date ('ymdhis '); // rename
If ($ yl_ext = 0 ){
$ Yl_file_names = $ yl_file_namenews. '.'. $ this-> yl_getfiletype (); // rename
} Elseif ($ yl_ext = 1 ){
$ Yl_file_names = policyl_file_namenews.'.jpg '; // change the name to jpg.
      }
$ Yl_tmp_name = $ this-> yl_getfilename ('tmp _ name'); // temporary file name on the server
$ Yl_file_size = $ this-> yl_getfilesize (); // get the file size
$ Yl_file_type = $ this-> yl_getfiletype (); // Obtain the file type
$ Yl_file_path = $ yl_directroy. '/'. $ yl_settingsnew; // create a directory
// Check whether the directory exists. If the directory does not exist, create
If (@ is_dir ($ yl_file_path) === false ){
$ This-> createfolder (''. $ yl_file_path.''); // create a directory
     }
// Check whether the object has been uploaded
If (@ is_uploaded_file ($ yl_tmp_name) === false ){
$ This-> alert ("The file has been uploaded! ");
Exit;
   }
// Check the directory write permission
If (@ is_writable ($ yl_file_path) === false ){
$ This-> alert ("the upload Directory has no write permission! ");
Exit;
     }
$ Yl_doupload = @ copy ($ yl_tmp_name, ''. $ yl_file_path. '/'. $ yl_file_names .'');
If ($ yl_doUpload === false)
   {
$ This-> alert ("Upload failed! ");
} Else {
Echo 'upload successful ';
Echo '<br> ';
Echo 'file Directory: '. $ yl_file_path .'';
Echo '<br> ';
Echo 'original File name: '. $ yl_file_nameold .'';
Echo '<br> ';
Echo 'new File name: '. $ yl_file_names .'';
Echo '<br> ';
Echo 'file size: '. $ yl_file_size .'';
Echo '<br> ';
Echo 'file type: '. $ yl_file_type .'';
   }
Return;
}
#*************************************** ******************
# * Delete an object
#*************************************** ******************
Function delfile ()
{
Global $ yl_filedata, $ yl_directroy, $ yl_settingsnew;
Global $ yl_maxsize, $ yl_sizeformat, $ yl_arrext, $ yl_ext;
$ Yl _ dir = dirname (trim ($ yl_directroy ));
If ($ this-> _ isDel ($ yl _ dir ))
   {
Return @ unlink ($ yl_directroy )? True: false;
} Else {
Return false;
   }
}
#*************************************** ******************
# Deleting files in a directory cannot be deleted
#*************************************** ******************
Function deldir ()
{
Global $ yl_filedata, $ yl_directroy, $ yl_settingsnew;
Global $ yl_maxsize, $ yl_sizeformat, $ yl_arrext, $ yl_ext;
If ($ this-> _ isdel ($ yl_directroy) & is_dir ($ yl_directroy ))
   {
Return @ rmdir ($ yl_directroy )? True: false;
} Else {
Return false;
   }
}
#*************************************** ******************
# Prompt
#*************************************** ******************
Function alert ($ yl_msg)
{
Echo 'Echo 'Echo '<title> error </title> ';
Echo '<meta http-equiv = "content-type" c> ';
Echo 'Echo '<body> ';
Echo '<script type = "text/javascript"> alert ("'. $ yl_msg. '"); </script> ';
Echo '</body> ';
Echo 'Exit;
}
}
?>

 

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.