-1

My question is almost the same as this one Deleting all files from a folder using PHP?

And I tried to use the answer provided by @Floern https://stackoverflow.com/a/4594262/2167772

But it didn't work for me. I tried to get filenames from a folder on the Linux server. And I have changed the folder and file permission to rwxrwxrwx. And I got the "Unable to get the filename" message all the time. Does anyone know how to solve it? Thanks a lot!

     $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!");
                    }       
                }   

------Update----

I just figured it out. It is the problem with the server. I cannot write anything to the data folder even I assign the write permission. I will move my in folder to another folder.

Thank you so much for everyone's comments!

Community
  • 1
  • 1
Ivy
  • 469
  • 1
  • 5
  • 9
  • So the question really is "why does `glob` fail"? How would we know? – Jon Jul 17 '14 at 18:16
  • use scandir() end exclude files that are directories with testing `is_dir($item)` – Bobot Jul 17 '14 at 18:17
  • 1
    do you actually have `/data` directory in the root of your file system? I doubt it... you probably have `/data` in your site's document root, but that's going to be `/some/other/path/site/docroot/data`, not just `/data` – Marc B Jul 17 '14 at 18:18
  • @Jon I don't think you understand my question. glob works for other people. So the question is not "why does glob fail". If I know what the question is, I will not post a question here. – Ivy Jul 17 '14 at 18:21
  • @user2167772: If you get "unable to get the filename" then that means `glob` fails. Which means that you want to find out why `glob` fails. There is only a single argument to `glob`. Are you sure it's correct? – Jon Jul 17 '14 at 18:23
  • I'm pretty sure its answered [HERE](http://stackoverflow.com/a/24810837/3799829) check answers cmon .. – Bobot Jul 17 '14 at 18:25

4 Answers4

0

As said @Marc B, you just have to write this :

 $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!");
                }       
            }

You write : glob('/data/in/*'), I write glob('data/in/*')

You use absolute path, i use relative path.

Bobot
  • 1,118
  • 8
  • 19
0

Use this tutorial: http://de3.php.net/manual/de/function.rmdir.php#107233

And check is your folder permission is enable to write.

Have you trying like this:

$dir = 'your/directory/';
foreach(glob($dir.'*.*') as $v){
    unlink($v);
}
ivanfromer
  • 329
  • 1
  • 5
0

if file is not at public directory...

change mode of file to 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!");
                }       
            }   
Muhammad Ali
  • 1,992
  • 1
  • 13
  • 20
0

solution with php. don't like the glob function. you can use the RecursiveDirectoryIterator

    $filePath = '/someDir';
    $directoryIterator = new \RecursiveDirectoryIterator(realpath($filePath));
    foreach (new \RecursiveIteratorIterator($directoryIterator) as $object ) {
        if(!$object->isDir())
        {
            unlink($object->getPath());
        }
    }
ins0
  • 3,918
  • 1
  • 20
  • 28