Flo*_*ern 607
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
Run Code Online (Sandbox Code Playgroud)
如果你想删除像'.htaccess这样的'隐藏'文件,你必须使用
$files = glob('path/to/temp/{,.}*', GLOB_BRACE);
Run Code Online (Sandbox Code Playgroud)
Sti*_*oza 253
如果你想删除一切从文件夹(包括子文件夹)使用这个组合array_map,unlink以及glob:
array_map( 'unlink', array_filter((array) glob("path/to/temp/*") ) );
Run Code Online (Sandbox Code Playgroud)
更新
这个调用也可以处理空目录 - 感谢提示,@ mojuba!
array_map( 'unlink', array_filter((array) glob("path/to/temp/*") ) );
Run Code Online (Sandbox Code Playgroud)
Yam*_*iko 87
这是使用标准PHP库(SPL)的更现代的方法.
$dir = "path/to/directory";
$di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
foreach ( $ri as $file ) {
$file->isDir() ? rmdir($file) : unlink($file);
}
return true;
Run Code Online (Sandbox Code Playgroud)
Jak*_*ris 68
foreach (new DirectoryIterator('/path/to/directory') as $fileInfo) {
if(!$fileInfo->isDot()) {
unlink($fileInfo->getPathname());
}
}
Run Code Online (Sandbox Code Playgroud)
Poe*_*rin 19
此代码来自http://php.net/unlink:
/**
* Delete a file or recursively delete a directory
*
* @param string $str Path to file or directory
*/
function recursiveDelete($str) {
if (is_file($str)) {
return @unlink($str);
}
elseif (is_dir($str)) {
$scan = glob(rtrim($str,'/').'/*');
foreach($scan as $index=>$path) {
recursiveDelete($path);
}
return @rmdir($str);
}
}
Run Code Online (Sandbox Code Playgroud)
Hai*_*vgi 14
$dir = 'your/directory/';
foreach(glob($dir.'*.*') as $v){
unlink($v);
}
Run Code Online (Sandbox Code Playgroud)
Sta*_*eXV 11
<?php
if ($handle = opendir('/path/to/files'))
{
echo "Directory handle: $handle\n";
echo "Files:\n";
while (false !== ($file = readdir($handle)))
{
if( is_file($file) )
{
unlink($file);
}
}
closedir($handle);
}
?>
Run Code Online (Sandbox Code Playgroud)
假设你有一个文件夹,其中包含大量文件,然后以两个步骤删除,那就不是这样了.我相信删除文件最有效的方法是使用系统命令.
例如在linux上我使用:
exec('rm -f '. $absolutePathToFolder .'*');
Run Code Online (Sandbox Code Playgroud)
或者如果你想要递归删除而不需要编写递归函数
exec('rm -f -r '. $absolutePathToFolder .'*');
Run Code Online (Sandbox Code Playgroud)
PHP支持的任何操作系统都存在相同的命令.请记住,这是一种删除文件的执行方式.在运行此代码之前,必须检查并保护$ absolutePathToFolder,并且必须授予权限.
小智 8
从PHP中删除文件夹中所有文件的简单而最好的方法
$files = glob('my_folder/*'); //get all file names
foreach($files as $file){
if(is_file($file))
unlink($file); //delete file
}
Run Code Online (Sandbox Code Playgroud)
从这里得到这个源代码 - http://www.codexworld.com/delete-all-files-from-folder-using-php/
unlinkr 函数通过确保它不会删除脚本本身来递归删除给定路径中的所有文件夹和文件。
function unlinkr($dir, $pattern = "*") {
// find all files and folders matching pattern
$files = glob($dir . "/$pattern");
//interate thorugh the files and folders
foreach($files as $file){
//if it is a directory then re-call unlinkr function to delete files inside this directory
if (is_dir($file) and !in_array($file, array('..', '.'))) {
echo "<p>opening directory $file </p>";
unlinkr($file, $pattern);
//remove the directory itself
echo "<p> deleting directory $file </p>";
rmdir($file);
} else if(is_file($file) and ($file != __FILE__)) {
// make sure you don't delete the current script
echo "<p>deleting file $file </p>";
unlink($file);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果要删除放置此脚本的所有文件和文件夹,请按如下方式调用它
//get current working directory
$dir = getcwd();
unlinkr($dir);
Run Code Online (Sandbox Code Playgroud)
如果您只想删除 php 文件,请按如下方式调用它
unlinkr($dir, "*.php");
Run Code Online (Sandbox Code Playgroud)
您也可以使用任何其他路径来删除文件
unlinkr("/home/user/temp");
Run Code Online (Sandbox Code Playgroud)
这将删除 home/user/temp 目录中的所有文件。