如何在php中删除名称上有特定单词的文件
在我的目录中,我有以下文件: JBBsongs.html、JBB.php、ArrangeJBB.html、Follow.html和Follow.php 我知道我可以使用如何在php中删除名称上有特定单词的文件,php,Php,在我的目录中,我有以下文件: JBBsongs.html、JBB.php、ArrangeJBB.html、Follow.html和Follow.php 我知道我可以使用取消链接 <?php unlink('test.html'); ?> 但是如何删除名称中包含“JBB”的特定文件?您可以使用readdir或scandir在文件夹中的所有文件之间循环。 一旦有了文件名,就可以检查JBB的strpo。如果strpos值等于或大于0。然后可以调用文件名上的unlink函数。这将删除
取消链接
<?php
unlink('test.html');
?>
但是如何删除名称中包含“JBB”的特定文件?您可以使用readdir或scandir在文件夹中的所有文件之间循环。
一旦有了文件名,就可以检查JBB的strpo。如果strpos值等于或大于0。然后可以调用文件名上的unlink函数。这将删除文件您可以使用readdir或scandir在文件夹中的所有文件之间循环。 一旦有了文件名,就可以检查JBB的strpo。如果strpos值等于或大于0。然后可以调用文件名上的unlink函数。这将删除文件使用功能 假设您的文件名已经存在于数组中,您可以执行以下操作
<?php
$files = Array ('JBBsongs.html', 'JBB.php', 'ArrangeJBB.html', 'Follow.html', 'follow.php');
foreach ($files as $file)
{
if (strstr ($file, "JBB"))
{
unlink('test.html');
}
}
?>
strstr
如果第一个字符串中不存在指针(搜索的字符串),则返回false
。这样,只有所需的文件才会被取消链接。使用该功能
假设您的文件名已经存在于数组中,您可以执行以下操作
<?php
$files = Array ('JBBsongs.html', 'JBB.php', 'ArrangeJBB.html', 'Follow.html', 'follow.php');
foreach ($files as $file)
{
if (strstr ($file, "JBB"))
{
unlink('test.html');
}
}
?>
strstr
如果第一个字符串中不存在指针(搜索的字符串),则返回false
。这样,只有所需的文件才会被取消链接。您可以使用scandir
在目录中循环,使用strpos
检查是否包含JBB
的文件,然后取消链接
$dir="./";
$files=scandir($dir);
if(is_array($files)){
foreach($files as $file){
if(is_file($dir.$file) && strpos($file,"JBB")!==false){
unlink($dir.$file);
}
}
}
您可以使用scandir
在目录中循环,并使用strpos
检查包含JBB
的文件,然后取消链接
$dir="./";
$files=scandir($dir);
if(is_array($files)){
foreach($files as $file){
if(is_file($dir.$file) && strpos($file,"JBB")!==false){
unlink($dir.$file);
}
}
}
我从没想过这个。非常感谢。我从没想过这个。非常感谢。