Php file directory operation functions

Source: Internet
Author: User
Tags mkdir parent directory

Mkdir (); function

Php creates folders and files

/// Create a folder

The code is as follows: Copy code
Function createdir ($ dir)
{
If (file_exists ($ dir) & is_dir ($ dir) {// if this file exists and this file is a directory, no action will be taken.
}
Else {
Mkdir ($ dir, 0777); // otherwise, this directory is created.
}
}

File_exists (); function

File_exists -- check whether a file or directory exists

The code is as follows: Copy code

<? Php
$ Filename = '/path/to/foo.txt ';

If (file_exists ($ filename )){
Print "The file $ filename exists ";
} Else {
Print "The file $ filename does not exist ";
}
?>

Is_dir (); function

Is_dir (); test whether the file is a directory.
If it is a directory, true is returned, and false is not returned.


Is_file ();

Is_File ('Destination file path and filename ')
Returns "True" for the file, not "False" for the file ".


Is_link ();
File system function library

Is_link (); test whether the file is a linked file.
True indicates that the specified filename exists and is a symbolic link file.

Rmdir command
Function: delete an empty directory.
Rmdir [option] dirname
-P recursively deletes the directory dirname. When the subdirectory is deleted and its parent directory is empty, it is also deleted. If the entire path is deleted or for some reason

If some paths are retained, the system displays the corresponding information on the standard output.

To clear and remove the Directory, enter:

Rm mydir/* mydir /.*
Rmdir mydir
This command removes the contents from the mydir file and then removes the empty directory. The rm command displays an error about trying to remove the directory. (dot) and. (dot, dot).

Then, run the rmdir command to remove the messages.


Create a file named aa.txt

The code is as follows: Copy code

Using fpw.fopen('aa.txt ', 'W +'); // create a new file
Chmod('aa.txt ', 0777); // sets the permission. Otherwise, it cannot be modified after creation. Delete it. // It seems that this line does not need to be used.
Fwrite ($ fp, 'content to write'); // write content
Fclose ($ fp); // close the file

Unlink () function
Function: delete an object.
Unlink ($ filename) deletes a file named $ filename.


Delete all objects in a specified directory

The code is as follows: Copy code

Function dir_clear ($ dir ){
$ Directory = dir ($ dir); // Create a dir class (as described in the Php Manual) to read every file in the directory.

Parts
While ($ entry = $ directory-> read () {// loop every file and get the file name $ entry
$ Filename = $ dir. '/'. $ entry; // obtain the complete file name, with the path
If (is_file ($ filename) {// if it is a file, delete it.
@ Unlink ($ filename );
        }
    }
$ Directory-> close (); // close the class for reading directory files
Result ();
}


List all file programs in directories that contain subdirectories

 

The code is as follows: Copy code

<? Php
/*
* PHP reads the file functions in the directory and its subcategories.
**/
$ Files = getFile ("images"); // call a function
Foreach ($ files as $ name ){
Echo "$ name". "<br> ";
}
 
Function getFile ($ dir ){
$ Files = array ();
If (is_file ($ dir) {// if the specified path is a file, directly return
Return $ dir;
 }
 
$ Hande = opendir ($ dir );
 
If ($ hande ){
While (false! ==( $ Ufile = readdir ($ hande) {// if the directory is read successfully
If ($ ufile! = "." & $ Ufile! = ".."){
$ Filename = $ dir. "/". $ ufile;
If (is_file ($ filename )){
$ Files [] = $ filename;
} Else {
$ Files = array_merge ($ files, getFile ($ filename ));
    }
   }
} // End While
Closedir ($ hande );
} // End if
 
Return $ files;
}
?>

This method is strongly recommended to delete all files in the specified directory and all folders and their own !!!
 

The code is as follows: Copy code
Function rmdir_tree ($ dirname) // defines a function rmdir_tree. The name of the object to be deleted is $ dirname.
{
$ Handle = opendir ($ dirname); // open the folder named $ dirname
While ($ file = readdir ($ handle) // run the loop to read the opened folder
{
If ($ file = ".") | ($ file = "..") continue; // conditions for continued running. if this condition is not met, it will not run.
$ Fullname = $ dirname. "/". $ file; // Complete file name (including path)
If (filetype ($ fullname) = "dir ")
Rmdir_tree ($ fullname); // call the defined function to implement recursion
Else
Unlink ($ fullname); // delete an object
}
Closedir ($ handle); // Close the opened folder
Rmdir ($ dirname); // delete itself (folder)
}

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.