To delete files in PHP, we need to use the unlink() file deletion function provided by PHP. Now I will give you a detailed introduction to how to use unlink to delete files. Friends in need can refer to this tutorial.
unlink(filename,context)
Example
The code is as follows |
Copy code |
代码如下 |
复制代码 |
if (unlink($file_delete)) {
echo "The file was deleted successfully.", "n";
} else {
echo "The specified file could not be deleted. Please try again.", "n";
}
|
if (unlink($file_delete)) {
echo "The file was deleted successfully.", "n";
} else {
echo "The specified file could not be deleted. Please try again.", "n";
}
|
Check whether the file exists
代码如下 |
复制代码 |
$myfile = "./test1.txt";
if (file_exists($myfile)) {
$result=unlink ($myfile);
echo $result;
}
?>
|
The code is as follows |
Copy code |
$myfile = "./test1.txt";
if (file_exists($myfile)) {
$result=unlink ($myfile);
echo $result;
}
?>
|
代码如下 |
复制代码 |
function delFileUnderDir( $dirName="../Smarty/templates/templates_c" )
{
if ( $handle = opendir( "$dirName" ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != "." && $item != ".." ) {
if ( is_dir( "$dirName/$item" ) ) {
delFileUnderDir( "$dirName/$item" );
} else {
if( unlink( "$dirName/$item" ) )echo "成功删除文件: $dirName/$item n";
}
}
}
closedir( $handle );
}
}delDirAndFile( 'www.bKjia.c0m');
|
Delete files in batches
The code is as follows |
Copy code |
function delFileUnderDir( $dirName="../Smarty/templates/templates_c" )
{
if ( $handle = opendir( "$dirName" ) ) {
while ( false !== ( $item = readdir( $handle ) ) ) {
if ( $item != "." && $item != ".." ) {
if ( is_dir( "$dirName/$item" ) ) {
delFileUnderDir( "$dirName/$item" );
} else {
if( unlink( "$dirName/$item" ) )echo "File deleted successfully: $dirName/$item n";
}
}
}
closedir( $handle );
}
}delDirAndFile( 'www.bKjia.c0m');
|
Example
Delete files in the directory and specify those not to be deleted
The code is as follows
代码如下 |
复制代码 |
header("content-Type: text/html; charset=utf-8");
//配置开始
$path=".";//在些设置所删除的目录.为当前目录 如:删除path目录,引号里请添path;
$guolv="del.php,install.php,path";//设置需要过滤的文件或文件夹用英文状态下,号分隔
//配置结束
if($_GET['action']=="del"){
$file= array_values_recursive(recurdir($path,$guolv));
foreach($file as $k => $v){
remove_directory($v);
}
}else{
echo "您的配置如下
要删除的目录为:
";
if($path==".")echo "当前目录";else echo $path;
echo " 您要过滤的文件或文件夹有:".$guolv."
如果确认过滤请点击此处开始删除相应的目录及目录下的所有文件,如果配置不正确请到文件中修改
";
}
//删除目录及文件
function remove_directory($dir) {
foreach(glob($dir) as $fn) {
echo " removing $fn n";
if (!is_writable($fn))@chmod($fn, 0777);
if(is_dir($fn)){@rmdir($fn);}else{@unlink($fn);}
}
}
//扫描目录
function recurdir($pathname,$guolv='del.php')
{
$result=array();$temp=array();
//检查目录是否有效和可读
if(!is_dir($pathname) || !is_readable($pathname))
return null;
//得到目录下的所有文件夹
$allfiles=scandir($pathname);
foreach($allfiles as $key => $filename)
{
//如果是“.”或者“..”的话则略过
if(in_array($filename,array('.','..')))continue;
if(count($guolv)>0){$lv=explode(",",$guolv);if(in_array($filename,$lv))continue;}
//得到文件完整名字
$fullname =$pathname . "/" .$filename;
//如果该文件是目录的话,递归调用recurdir
$temp[]=$fullname;
if(is_dir($fullname)){
$nowpath=explode("/",$fullname);
if(count($guolv)>0){$lv=explode(",",$guolv);if(in_array($nowpath[count($nowpath)-1],$lv))continue;}
$result[$filename] = recurdir($fullname);}
}
//最后把临时数组中的内容添加到结果数组,确保目录在前,文件在后
foreach($temp as $f){
$result[]=$f;
}
return $result;
}
//获取所有文件
function array_values_recursive($ary)
{
$lst = array();
foreach( array_keys($ary) as $k ){
$v = $ary[$k];
if (is_array($v)) {$lst = array_merge( $lst, array_values_recursive($v));}else{$lst[] = $v;}
}
return $lst;
}
?>
|
|
Copy code |
|
header("content-Type: text/html; charset=utf-8");
//Configuration starts
$path=".";//The directory deleted in these settings is the current directory. For example: to delete the path directory, please add path in quotation marks;
$guolv="del.php,install.php,path";//Set the files or folders that need to be filtered in English, separated by ampersand
//Configuration ends
if($_GET['action']=="del"){
$file= array_values_recursive(recurdir($path,$guolv));
foreach($file as $k => $v){
remove_directory($v);
}
}else{
echo "Your configuration is as follows
The directory to be deleted is:
";
if($path==".")echo "current directory";else echo $path;
echo "
The files or folders you want to filter are: ".$guolv."
If you confirm the filtering, please
Click here to start deleting the corresponding directory and all files in the directory. If the configuration is incorrect, please modify it in the file
";
}
//Delete directories and files
function remove_directory($dir) {
foreach(glob($dir) as $fn) {
echo " removing $fn
n";
if (!is_writable($fn))@chmod($fn, 0777);
if(is_dir($fn)){@rmdir($fn);}else{@unlink($fn);}
}
}
//Scan directory
function recurdir($pathname,$guolv='del.php')
{
$result=array();$temp=array();
//Check whether the directory is valid and readable
if(!is_dir($pathname) || !is_readable($pathname))
return null;
//Get all folders in the directory
$allfiles=scandir($pathname);
foreach($allfiles as $key => $filename)
{
//If it is "." or "..", skip it
if(in_array($filename,array('.','..')))continue;
if(count($guolv)>0){$lv=explode(",",$guolv);if(in_array($filename,$lv))continue;}
//Get the complete name of the file
$fullname =$pathname . "/" .$filename;
//If the file is a directory, call recurdir recursively
$temp[]=$fullname;
if(is_dir($fullname)){
$nowpath=explode("/",$fullname);
if(count($guolv)>0){$lv=explode(",",$guolv);if(in_array($nowpath[count($nowpath)-1],$lv))continue;}
$result[$filename] = recurdir($fullname);}
}
//Finally, add the contents of the temporary array to the result array, ensuring that the directory is in the front and the file is in the back
foreach($temp as $f){
$result[]=$f;
}
return $result;
}
//Get all files
function array_values_recursive($ary)
{
$lst = array();
foreach( array_keys($ary) as $k ){
$v = $ary[$k];
if (is_array($v)) {$lst = array_merge( $lst, array_values_recursive($v));}else{$lst[] = $v;}
}
Return $lst;
}
?>
http://www.bkjia.com/PHPjc/633078.htmlwww.bkjia.com
trueTechArticleTo delete files in php we need to use the unlink() file deletion function provided by php. Let me tell you Detailed introduction to how to delete files using unlink, friends in need can refer to this tutorial...