PHP Create a multi-level directory complete Encapsulation class method

Source: Internet
Author: User
This article mainly introduces PHP to create a multi-level directory of complete package class method, interested in the friend's reference, I hope to help you.

Create a function in a multilevel directory function that invokes the specified file under create specified:

Public Function Create_dir ($dir, $mode =0777)  {    return Is_dir ($dir) or ($this->create_dir (dirname ($dir)) and mkdir ($dir, $mode));  }

Creates the specified file under the specified path, and the string needs to include the file name and suffix path (which needs to include the file name and suffix), booleanover_write whether to overwrite files, and int to set the time. The default is the time the current system time is set. The default is the current system time, Intatime set access time. The default is the current system time. The function called Create_dir to create a directory function and delete the Unlink_file function as follows:

Public Function Create_file ($path, $over _write=false, $time =null, $atime =null)  {    $path = $this->dir_ Replace ($path);    $time = Empty ($time)? Time (): $time;    $atime = Empty ($atime)? Time (): $atime;    if (file_exists ($path) && $over _write)    {      $this->unlink_file ($path);    }    $aimDir = DirName ($path);    $this->create_dir ($aimDir);    Return Touch ($path, $time, $atime);  }

Delete non-empty directory functions, Description: Can only delete non-system and specific permissions of the file, otherwise an error occurs, the string directory path dirname directory path, booleanis_all whether to delete all, Boolean $del _dir whether to delete the directory, the function is as follows:

Public Function Remove_dir ($dir _path, $is _all=false)  {    $dirName = $this->dir_replace ($dir _path);    $handle = @opendir ($dirName);    while (($file = @readdir ($handle))!== FALSE)    {      if ($file! = '. ' && $file! = ' ... ')      {        $dir = $dirName. '/' . $file;        if ($is _all)        {          is_dir ($dir)? $this->remove_dir ($dir): $this->unlink_file ($dir);        }        else         {          if (Is_file ($dir))          {            $this->unlink_file ($dir);    }}} Closedir ($handle);    Return @rmdir ($dirName);  }

Replace the character function corresponding to//in the path:

Public Function Dir_replace ($path)  {    return str_replace ('///', '/', str_replace (' \ \ ', '/', $path));  }

/**   * Specify file Encoding conversion   * @param string $path file path   * @param string $input _code original Encoding   * @param string $out _code output encoding c4/>* @return Boolean   */public  function Change_file_code ($path, $input _code, $out _code)  {    if (is_ File ($path))//Check the existence of files, if present to execute transcoding, return true    {      $content = file_get_contents ($path);      $content = String::chang_code ($content, $input _code, $out _code);      $fp = fopen ($path, ' W ');      Return fputs ($fp, $content)? True:false;      Fclose ($FP);    }  }

/**
* Get upload file information
* @param $file File property information
* @return Array
*/

Public Function Get_upload_file_info ($file)  {    $file _info = $_files[$file];//Get the basic information of the uploaded file    $info = Array ();    $info [' type '] = Strtolower (Trim (stripslashes (preg_replace) ("/^ (. +?)";. *$/"," \\1 ", $file _info[' type ')));//Get file type    $info [' temp '] = $file _info[' tmp_name '];//get upload file temporarily save directory in server    $info [' size '] = $file _info[' size '];//get the upload file sizes    $info [' error '] = $file _info[' ERROR '];//get file Upload error    $info [' Name '] = $file _info[' name '];//get the upload    file name $info [' ext ']  = $this->get_ext ($file _info[' name ']);//Get upload suffix    return $info;  }

/**
* Get file path information
* @param $full _path full path
* @return Arrayobject
*/

The Public Function Get_file_type ($path)  {    //pathinfo () function returns information about the file path in the form of an array.    //---------$file _info = PathInfo ($path); Echo file_info[' extension ';----------//    //extension get file suffix name " PathInfo ($path, pathinfo_extension) "-----DIRNAME Get File path" PathInfo ($path, Pathinfo_dirname) "-----basename get file Full filename" PathInfo ($path, pathinfo_basename) "-----filename" pathinfo ($path, Pathinfo_filename) "    return PathInfo ($ path);  }

/**
* Returns information for specified files and directories
* @param string $file
* @return Arrayobject
*/

Public Function List_info ($file) {$dir = array ();    $dir [' filename '] = basename ($file);//returns the file name portion of the path.    $dir [' pathname '] = Realpath ($file);//returns the absolute path name.    $dir [' owner '] = Fileowner ($file);//the User ID (owner) of the file.    $dir [' perms '] = fileperms ($file);//Returns the inode number of the file.    $dir [' inode '] = Fileinode ($file);//Returns the inode number of the file.    $dir [' group '] = filegroup ($file);//Returns the group ID of the file.    $dir [' path '] = DirName ($file);//Returns the directory name part of the path.    $dir [' atime '] = Fileatime ($file);//Returns the last access time of the file.    $dir [' ctime '] = Filectime ($file);//Returns the last time the file was changed.     $dir [' perms '] = fileperms ($file);//Returns the file's permissions.    $dir [' size '] = filesize ($file);//returns the file size.    $dir [' type '] = filetype ($file);//returns the file type. $dir [' ext '] = Is_file ($file)?    PathInfo ($file, pathinfo_extension): ';//returns the file suffix $dir [' mtime '] = Filemtime ($file);//returns the last modified time of the file.    $dir [' isdir '] = Is_dir ($file);//Determines whether the specified file name is a directory.    $dir [' isfile '] = Is_file ($file);//Determines whether the specified file is a regular file.    $dir [' islink '] = Is_link ($file);//Determines whether the specified file is a connection. $dir [' IsreadAble '] = is_readable ($file);//Determine if the file is readable.    $dir [' iswritable '] = is_writable ($file);//Determine if the file is writable.    $dir [' isupload '] = Is_uploaded_file ($file);//Determine if the file was uploaded via HTTP POST.  return $dir; }

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.