This class is a file operation class that implements file creation, writing, deletion, modification, copying, moving, creating directories, and deleting directories.
/**
*This class is a file operation class, which implements the creation, writing, deletion, modification, copying, moving, creating directories, and deleting directories of files.
* List the files in the directory and other functions. Don’t forget to add "/" after the path. "
*
* @author passerby Hao
* @copyright myself
* @link www.phpr.cn
*
*/
class fileoperate
{
var path;//File path
var name;//File name
var result;//Result of file operation
/**
* This method is used to create the name file in the path directory
*
* @param string path
* @param string name
*/
function creat_file(path,name)//Create file
{
filename=path.name;
if (file_exists(filename))
{
echo "The file already exists, please change it" File name";
}
else
{
if (file_exists(path))
{
touch(name);
rename(name,filename);
echo "File created successfully";
}
else{
echo "The directory does not exist, please check";
}
}
}
/**
* This method is used to write files. Write the content content to the name file under the path path. bool is the write option. When the value is 1,
* Then continue writing under the original content of the file. When the value is 2, the file after writing Only this content content
*
* @param string_type path
* @param string_type name
* @param string_type content
* @param bool_type bool
*/
function write_file(path,name,content,bool) //Write file
{
filename= path.name;
if (bool==1) {
if (is_writable(filename)) {
handle=fopen(filename,'a');
if (!handle) {
echo "The file cannot be opened or the file cannot be opened" Exists";
exit;
}
result=fwrite(handle,content);
if (!result) {
echo "File writing failed";
}
echo "File writing successful";
fclose(handle) ;
}
else echo "File does not exist";
}
if (bool==2) {
if (!file_exists(filename)) {
this->creat_file(path,name);
handle=fopen( filename,'a');
if (fwrite(handle,content));
echo "File written successfully";
}
else {
unlink(filename);
this->creat_file(path,name) ;
this->write_file(path,name,content,1);
echo "File modified successfully";
}
}
}
/**
* This method deletes the name file under the path
*
* @param string_type path
* @param string_type name
*/
function del_file(path,name) { //Delete file
filename=path.name;
if (!file_exists(filename)) {
echo "The file does not exist, please confirm whether the path is correct";
}
else {
if (unlink(filename)) {
echo "File deletion successful";
}
else echo "File deletion failed";
}
}
/**
* This method is used to modify the contents of the name file in the path directory (visible)
*
* @param string_type path
* @param string_type name
*/
function modi_file(path,name){ //File modification
filename =path.name;
handle=fopen(filename,'r+');
content=file_get_contents(filename);
echo "
echo "File content";
echo "
";
echo "File path
";
echo "";
echo "}
/**
* This method is used to copy the name file from spath to dpath
*
* @param string name
* @param string spath
* @param string dpath
*/
function copy_file(name,spath,dpath) //File copy
{
filename=spath.name;
if (file_exists(filename)) {
handle= fopen(filename,'a');
copy(filename,dpath.name);
if (file_exists(dpath.name))
echo "File copy successful";
else echo "File copy failed";
}
else echo "File does not exist";
}
/**
* This method moves the name file from spath to path
*
* @param string_type path
* @param string_type dirname
* @param string_type dpath
*/
function move_file(name,spath,dpath) //Move file
{
filename=spath.name;
if (file_exists(filename)) {
result=rename(filename,dpath.name);
if (result==false or !file_exists(dpath))
echo "File movement failed or the destination directory does not exist";
else
echo "File movement successful";
}
else {
echo "File does not exist and cannot be moved";
}
}
}
?>
Copy code
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn