This class is the file operation class, implements the file establishment, writes, deletes, modifies, duplicates, moves, creates the directory, deletes the directory
- /**
- * This class is the file operation class, implements the file establishment, writes, deletes, modifies, copies, moves, creates the directory, deletes the directory
- * List the files in the directory and other functions, do not forget to add "/" after the path
- *
- * @author Lu Shin
- * @copyright Myself
- * @link www.phpr.cn
- *
- */
- Class Fileoperate
- {
- var path;//file path
- var name;//file name
- var result;//results after 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)//Build file
- {
- Filename=path.name;
- if (file_exists (filename))
- {
- echo "File already exists, please change the filename";
- }
- Else
- {
- if (file_exists (path))
- {
- Touch (name);
- Rename (name,filename);
- echo "File established successfully
";
- }
- else{
- echo "Directory does not exist, please check";
- }
- }
- }
- /**
- * This method is used to write the file, to the path path under the name file to write content, bool is the Write option, the value is 1 o'clock
- * Next to the original content of the file continues to write, the value is 2 o'clock after the file is written only this 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 "file cannot be opened or the file does not exist";
- Exit
- }
- Result=fwrite (handle,content);
- if (!result) {
- echo "File failed to write";
- }
- echo "File write succeeded";
- 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 write succeeded";
- }
- else {
- unlink (filename);
- This->creat_file (Path,name);
- This->write_file (path,name,content,1);
- echo "File modified successfully";
- }
- }
- }
- /**
- * This method deletes the path path under the name file
- *
- * @param string_type Path
- * @param string_type Name
- */
- function Del_file (path,name) {//delete file
- Filename=path.name;
- if (!file_exists (filename)) {
- echo "File does not exist, please confirm the path is correct";
- }
- else {
- if (unlink (filename)) {
- echo "File deletion succeeded";
- }
- else echo "File deletion failed";
- }
- }
- /**
- * This method is used to modify the contents of the name file in the path directory (visual)
- *
- * @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 "";
- }
- /**
- * 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 succeeded";
- 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 move failed or destination directory does not exist";
- Else
- echo "File moved successfully";
- }
- else {
- echo "File does not exist, cannot be moved";
- }
- }
- }
- ?>
Copy Code |