PHP File Action Class

Source: Internet
Author: User
<?php


/**
* ============================================================================
* File Action Class
* ============================================================================
* @author Aiden
* @version 1.0
*/
Class File
{

/**
* Create Folder
*
* @param string $path folder path
*/
public static function CreateFolder ($path)
{
if (!file_exists ($path))
{
Self::createfolder (DirName ($path));
mkdir ($path, 0777);
}
}


/**
* Get the information in the specified directory
*
* @return Unknown
*/
public static function GetFolder ($path)
{
if (!is_dir ($path))
return null;

$path = RTrim ($path, '/'). ' /';

$array = Array (' file ' =>array (), ' folder ' =>array ());
$dir _obj = Opendir ($path);

while ($dir = Readdir ($dir _obj))
{
if ($dir!= '. ' && $dir!= ' ... ')
{
$file = $path. $dir;
if (Is_dir ($file))
$array [' folder '] = $dir;
ElseIf (Is_file ($file)) {
$array [' file '] = $dir;
}

}
}
Closedir ($dir _obj);
return $array;

}


/**
* Delete File
*
* @param string $path file path
*/
public static function Delfile ($path)
{
if (file_exists ($path))
{
@unlink ($path);
}
}

/**
* Delete Directory
*
* @param string $dir directory path
*/
public static function Deletedir ($path)
{
if (!is_dir ($path))
return;

$path = RTrim ($path, '/'). ' /';

$dir _obj = Opendir ($path);

while ($dir = Readdir ($dir _obj))
{
if ($dir!= '. ' && $dir!= ' ... ')
{
$file = $path. $dir;
if (Is_dir ($file))
Self::d eletedir ($file);
ElseIf (Is_file ($file)) {
Unlink ($file);
}

}
}
Closedir ($dir _obj);
RmDir ($path);
}

/**
* Copy Directory
*
* @param string $source directory address to copy
* @param string $destination destination directory address
* @param int $child whether to copy subdirectories
* @return BOOL
*/
public static function XCopy ($source, $destination, $child =1)
{
if (!is_dir ($source))
{
Echo ("Error:the $source is not a direction!");
return 0;
}
if (!is_dir ($destination))
{
mkdir ($destination, 0777);
}
$handle =dir ($source);
while ($entry = $handle->read ())
{
if ($entry!= ".") && ($entry!= "..."))
{
if (Is_dir ($source.) /". $entry))
{
if ($child)
Self::xcopy ($source. " /". $entry, $destination." /". $entry, $child);
}
Else
{
Copy ($source. " /". $entry, $destination." /". $entry);

}

}
}
return 1;
}

/**
* Copy Files
*
* @param string $source directory address to copy
* @param string $destination destination directory address
* @return NULL
*/
public static function CopyFile ($source, $path)
{
$path = str_replace (' \ \ ', '/', $path);
$arr = explode ('/', $path);
Array_pop ($arr);
$folder = Join ('/', $arr);
if (!is_dir ($folder)) {
Self::createfolder ($folder);
}
if (Is_file ($source)) {
Copy ($source, $path);
}
}


/**
* Create File
*
* @param unknown_type $file
*/
public static function CreateFile ($file, $content = ')
{
$folder = DirName ($file);
if (!is_dir ($folder)) {
Self::createfolder ($folder);
}

File_put_contents ($file, $content);
}
}


File::createfile (' a.php ', ' OK '); a.php-File path ok-file content, this is my encapsulation class, createfile-create file createfolder-These two methods need to use, the other you can refer to.
?>

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.