Home > Backend Development > PHP Tutorial > Detailed explanation of deleting unlink() in php files_PHP tutorial

Detailed explanation of deleting unlink() in php files_PHP tutorial

WBOY
Release: 2016-07-13 10:44:35
Original
1261 people have browsed it

Deleting files in php is very simple. Just use the unlink function to complete the deletion. If you want to delete all files in a directory, we need to use recursive operation to delete the directory.

Remember the lesson from PHP file creation, we created a file called testFile.txt.

The code is as follows
 代码如下 复制代码

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);
判断是否删除了.

$myFile = "testFile.txt";
unlink($myFile);

Copy code

 代码如下 复制代码

$filename = 'file.txt';
fopen($filename,'a+');
if(!unlink($filename))
{
echo "文件{$filename}删除失败";
}
else
{
echo "文件{$filename}删除成功";
}
?>

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);
Determine whether it has been deleted.

$myFile = "testFile.txt";
unlink($myFile);
 代码如下 复制代码

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 );
}
}

Example

The code is as follows
Copy code
$filename = 'file.txt';
fopen($filename,'a+');
if(!unlink($filename))
{
echo "File {$filename} deletion failed";
}
else
{
echo "File {$filename} was deleted successfully";
}
?>
Delete all files in the directory
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 );
}
}
http://www.bkjia.com/PHPjc/633098.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633098.htmlTechArticleDeleting files in php is very simple. Just use the unlink function to complete the deletion. If you want to delete all files in the directory, we It is necessary to use recursive operation directory to delete. Remember to start from the PHP file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template