使用PHP从Linux服务器的文件夹中删除所有文件


deleting all files from a folder on Linux server using PHP

我的问题几乎和这个一样,使用PHP从文件夹中删除所有文件?

我试着用@Floern https://stackoverflow.com/a/4594262/2167772提供的答案

但这对我不起作用。我试图从Linux服务器上的一个文件夹中获取文件名。并且我已经将文件夹和文件权限更改为rwxrwxrwx。我总是收到"无法获取文件名"的信息。有人知道怎么解吗?非常感谢!

     $files=glob('/data/in/*') or die ("Unable to get the filename");
                foreach ($files as $file) {
                    if(is_file($file)){
                        echo $file;
                        unlink($file) or die ("Uable to delete file!");
                    }       
                }   

——更新

我刚刚明白了。这是服务器的问题。即使我分配了写权限,我也不能向数据文件夹写入任何内容。我将我的in文件夹移动到另一个文件夹。

非常感谢大家的评论!

正如@Marc B所说,你只需要这样写:

 $files=glob('data/in/*') or die ("Unable to get the filenames!");
            foreach ($files as $file) {
                if(is_file($file)){
                    echo $file;
                    unlink($file) or die ("Unable to delete file!");
                }       
            }

你写:glob('/data/in/*'),我写glob('data/in/*')

你用绝对路径,我用相对路径

使用本教程:107233年http://de3.php.net/manual/de/function.rmdir.php

并检查您的文件夹是否允许写入

你试过这样吗?

$dir = 'your/directory/';
foreach(glob($dir.'*.*') as $v){
    unlink($v);
}

如果文件不在公共目录…

更改文件模式为777

  $files=glob('data/in/*') or die ("Unable to get the filename");
            foreach ($files as $file) {
                if(is_file($file)){
                    echo $file;
                       chmod($file,777);
                    unlink($file) or die ("Uable to delete file!");
                }       
            }   

解决方案。我不喜欢glob功能。你可以使用RecursiveDirectoryIterator

    $filePath = '/someDir';
    $directoryIterator = new 'RecursiveDirectoryIterator(realpath($filePath));
    foreach (new 'RecursiveIteratorIterator($directoryIterator) as $object ) {
        if(!$object->isDir())
        {
            unlink($object->getPath());
        }
    }