0

I would like to empty a folder (with chmod 777) before uploading a file :

$files = glob($targetdir); // get all file names
        foreach($files as $file)
        { // iterate files
          if(is_file($file))
            unlink($file); // delete file
        }

My folder has a right path builded with this code :

$targetdir = 'cartes_identites/'.$mds['nom'].' '.$mds['prenom'].'_verso';

My folder still full of files, I have to keep only one, uploaded with this code :

if (move_uploaded_file($_FILES['id_card_recto']['tmp_name'], $targetfile))
        {
          echo "file uploaded succeeded";
        }
David
  • 427
  • 1
  • 5
  • 21
  • I think you need a `/.*` or something in there. Basically a wildcard. I dont use `glob` much as I prefer using the SPL classes. – ArtisticPhoenix Mar 16 '19 at 10:20

2 Answers2

0

apply file path .

 $targetdir = 'cartes_identites/'.$mds['nom'].' '.$mds['prenom'].'_verso/*.*';
umesh bhanderi
  • 641
  • 8
  • 12
0

If it's about emptying entire folder, instead loop use simple Linux command:

if(is_dir($targetdir)){
    $output = shell_exec("rm -rf ".escapeshellarg($targetdir)."/*");
    echo "<pre>$output</pre>";
}

Make sure your target directory is absolutely correct.

Sky
  • 90
  • 7
  • 2
    `escapeshellarg()` - might be a nice addition, never know what `$targetdir` is, maybe it's a command to add a user etc. http://php.net/manual/en/function.escapeshellarg.php > `Escape a string to be used as a shell argument` – ArtisticPhoenix Mar 16 '19 at 10:22
  • @ArtisticPhoenix Agree, editing answer. – Sky Mar 16 '19 at 10:28
  • Thank you, it does not work. I'm sure about the path of the directory. My console shows this "
    – David Mar 16 '19 at 10:43
  • Can you just run "rm -rf dir" command in your console with your directory path, and let us know the result. – Sky Mar 16 '19 at 10:49