Example
Public function del (){
Header ("Content-Type: text/html; charset = UTF-8 ");
Echo 'click the file name to view: <br> ';
$ Dir = getcwd (). "/html /";
// Obtain all files and directory names in a directory (excluding files and directory names in subdirectories)
$ Handler = opendir ($ dir );
While ($ filename = readdir ($ handler ))! = False) {// be sure to use it! = To prevent files in directories with a file name similar to "0"
If ($ filename! = "." & $ Filename! = ".."){
$ Files [] = $ filename;
}
}
Closedir ($ handler );
// Print all file names
Foreach ($ files as $ value ){
$ Url = 'http: // '. $ _ SERVER ['http _ host']. "/html/". $ value;
Echo "<a href = '". $ url. "'target = '_ blank'> ". $ value. "</a> | <a href = '/index. php? S =/Index1/dodel/name/". $ value." 'target = '_ self'> delete </a> <br/> ";
}
}
Public function dodel (){
Header ("Content-Type: text/html; charset = UTF-8 ");
$ Fname = $ this-> _ get ("name ");
$ Fname = getcwd (). "/html/". $ fname. ". html ";
If (unlink ($ fname )){
Echo $ fname. 'file deleted! <A href = "javascript: history. go (-1);"> Return </a> ';
} Else {
Echo $ fname. 'deletion failed! <A href = "javascript: history. go (-1);"> Return </a> ';
}
}
Obtain all files in the directory, including subdirectories.
| The code is as follows: |
Copy code |
Function get_allfiles ($ path, & $ files ){
If (is_dir ($ path )){
$ Dp = dir ($ path );
While ($ file = $ dp-> read ()){
If ($ file! = "." & $ File! = ".."){
Get_allfiles ($ path. "/". $ file, $ files );
}
}
$ Dp-> close ();
}
If (is_file ($ path )){
$ Files [] = $ path;
}
}
Function get_filenamesbydir ($ dir ){
$ Files = array ();
Get_allfiles ($ dir, $ files );
Return $ files;
}
$ Filenames = get_filenamesbydir ("static/image /");
// Print all file names, including paths
Foreach ($ filenames as $ value ){
Echo $ value. "<br/> ";
} |
Php deletes all files in folders and their folders.
| The code is as follows: |
Copy code |
Function deldir ($ dir ){
// Delete the files in the directory first:
$ Dh = opendir ($ dir );
While ($ file = readdir ($ dh )){
If ($ file! = "." & $ File! = ".."){
$ Fullpath = $ dir. "/". $ file;
If (! Is_dir ($ fullpath )){
Unlink ($ fullpath );
} Else {
Deldir ($ fullpath );
}
}
}
Closedir ($ dh );
// Delete the current folder:
If (rmdir ($ dir )){
Return true;
} Else {
Return false;
}
} |