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 that allows you to create, write, delete, modify, copy, move, create, and delete files.
- * List files and other functions in the directory. do not forget to add "/" to the path "/"
- *
- * @ Author passers-by Hao
- * @ Copyright myself
- * @ Link www.phpr.cn
- *
- */
- Class fileoperate
- {
- Var path; // file path
- Var name; // file name
- Var result; // The result after the file operation
- /**
- * This method is used to create a name file in the path directory.
- *
- * @ Param string path
- * @ Param string name
- */
- Function creat_file (path, name) // create a file
- {
- Filename = path. name;
- If (file_exists (filename ))
- {
- Echo "the file already exists. please change the file name ";
- }
- Else
- {
- If (file_exists (path ))
- {
- Touch (name );
- Rename (name, filename );
- Echo "file created successfully
";
- }
- Else {
- Echo "the directory does not exist. check ";
- }
- }
- }
- /**
- * This method is used to write files and write content to the name file in the path. bool is the write option. if the value is 1
- * Write the original content of the file. if the value is 2, only the content of this file is written.
- *
- * @ Param string_type path
- * @ Param string_type name
- * @ Param string_type content
- * @ Param bool_type bool
- */
- Function write_file (path, name, content, bool) // write a 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 does not exist ";
- Exit;
- }
- Result = fwrite (handle, content );
- If (! Result ){
- Echo "file writing failed ";
- }
- Echo "file written successfully ";
- Fclose (handle );
- }
- Else echo "the 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 ";
- }
- }
- }
- /**
- * This method deletes the name file in the path.
- *
- * @ Param string_type path
- * @ Param string_type name
- */
- Function del_file (path, name) {// delete an object
- Filename = path. name;
- If (! File_exists (filename )){
- Echo "the file does not exist. check whether the path is correct ";
- }
- Else {
- If (unlink (filename )){
- Echo "the file is successfully deleted ";
- }
- Else echo "failed to delete the file ";
- }
- }
- /**
- * This method is used to modify the content (visible) in the name file in the path directory)
- *
- * @ 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) // copy a file
- {
- Filename = spath. name;
- If (file_exists (filename )){
- Handle = fopen (filename, 'A ');
- Copy (filename, dpath. name );
- If (file_exists (dpath. name ))
- Echo "file copied ";
- Else echo "failed to copy the file ";
- }
- Else echo "the file does not exist ";
- }
- /**
- * This method moves the name file from spath to the path
- *
- * @ Param string_type path
- * @ Param string_type dirname
- * @ Param string_type dpath
- */
- Function move_file (name, spath, dpath) // move the file
- {
- Filename = spath. name;
- If (file_exists (filename )){
- Result = rename (filename, dpath. name );
- If (result = false or! File_exists (dpath ))
- Echo "failed to move the file or the target directory does not exist ";
- Else
- Echo "file moved successfully ";
- }
- Else {
- Echo "the file does not exist and cannot be moved ";
- }
- }
- }
- ?>
|