我有这个脚本从目录中获取第一个文件名,这个变量从文件中删除一行(注册文件),然后从目录中删除实际文件,我需要采取这个操作只有当目录中有文件时才会放置,如果有多个文件,则继续使用第二个,第三个文件进行操作,依此类推,直到目录为空...怎么办?请帮助....
// Register file
$registerf = "results/register.php";
// Get the firs file name
$firstFile = scandir("results/todel/")[2];
// Delete it from register file
$regex = "'/$firstFile/d'";
$cmd = "sed -i $regex $registerf";
shell_exec($cmd);
// Actually delete the file from directory
unlink("results/todel/".$firstFile);
答案 0 :(得分:1)
// Register file
$registerf = "results/register.php";
//go through each file in dir
foreach(scandir("results/todel/") as $file){
if ($file != "." && $file != ".."){
// Delete it from register file
$regex = "'/$file/d'";
$cmd = "sed -i $regex $registerf";
shell_exec($cmd);
// Actually delete the file from directory if it's a file
unlink("results/todel/".$file);
}
}
答案 1 :(得分:0)
if ($handle = opendir('/mydir/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
unlink("/mydir/".$file);
}
}
closedir($handle);
}
希望这可以帮助你