[Original]php+ajax to realize the simulation win file management system
//
This tutorial by the original site, reproduced please specify the place
Author: www.111cn.net
Email: drise@163.com
qq:271728967
//
The function of the Deldir () function is to delete the file
function Deldir ($dir) {
if (Is_dir ($dir)) {
$rdir = $dir;
if ($dirlist = Scandir ($rdir)) {//scan directory
Array_shift ($dirlist); remove ".."
Array_shift ($dirlist); remove "."
foreach ($dirlist as $d) {
$rd = $rdir. ' /'. $d;
if (Isset ($d) && is_file ($rd)) {
Unlink ($RD);//delete file
}else{
Deldir ($RD);//recursion
}
}
RmDir ($rdir);//Delete Empty directory
}else{
return false;
}
}
return true;
}
This function also uses the recursive algorithm, carries on the directory and the file deletion,
Here is the filename () function, which is the function of renaming the file
function Filename ($path, $nname) {
Get old file name
if ($path = = "" | | SUBSTR ($path,-1) = = "/" | | Strlen ($path) >255) {
Exit (' illegal operation! ');
}else{
$oname = substr ($path, Strrpos ($path, "/") +1);
$opath = substr ($path, 0,strlen ($path)-strlen (substr ($path, Strrpos ($path, "/") +1))//Get the current path of the file
}
Renaming
if (Preg_match ("/^\w{1,255}\.\w{1,8}$/i", $nname, $temp _name) && preg_match ("/^\w{1,255}\.\w{1,8}$/i", $ Oname, $otemp _name)) {//matching filename
Rename_file_folder ($path, $opath, $nname)//Some functions are mentioned below
}else if (Preg_match ("/^\w{1,255}$/i", $nname, $temp _name) && preg_match ("/^\w{1,255}$/i", $oname, $otemp _name ) {///Match folder name
Rename_file_folder ($path, $opath, $nname);
}else{
Echo ("File info Error");
}}
function Rename_file_folder ($path, $opath, $newname) {
if (Is_dir ($path)) {
if (is_writable ($path)) {
if (Is_dir ($opath. $newname)) {exit ("Sorry dir is exists");}
echo rename ($path, $opath. $newname)? ' Rename Success ': ' Rename Fail Error ';
}else{
Echo (' can\ ' t Rename dir not is_writable ');
}
}else{
if (file_exists ($path) && is_writable ($path)) {
if (File_exists ($opath. $newname)) {exit (' file exists ');}
echo rename ($path, $opath. $newname)? ' Rename success ': ' Rename fail ';
}else{
echo "file can ' t is_writable";
}
}
}
Previous article