Précédent   Forum des professionnels en informatique > PHP > Langage > Fichiers
Fichiers Forum d'entraide sur les fichiers avec PHP. Avant de poster -> FAQ fichiers et Sources fichiers
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 22/09/2007, 22h31   #1
Invité de passage
 
Inscription : juillet 2006
Messages : 21
Détails du profil
Informations forums :
Inscription : juillet 2006
Messages : 21
Points : 3
Points : 3
Par défaut [FTP] suppression de fichier récursive d'un dossier

Bonjour
je cherche à créer une fonction de suppression récursive avec unlink() et rmdir();

Voici ma fonction :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
function del_tree ($path) {
  if (is_dir($path))
   {
     if ($dh = @opendir($path)) {
       while (($file = readdir($dh)) !== false) {
           if (($file != '.') && ($file != '..') && ($file != '...'))
                    if (is_dir($path . $file))
                    {
                    del_tree ($path . $file ."/");
                    }
                    else
                    {
                    unlink ($path . $file);
                    }
       }
       closedir($dh);
       }
    rmdir ($path);
   }
  elseif (is_file($path)) 
  {
    unlink ($path);
  }
 
}
Elle supprime tous les fichier, mais pas les dossier :-(
Je sais pas trop comment me débrouiller avec le rmdir();

Merci
pfrib est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/09/2007, 22h41   #2
Membre émérite
 
Inscription : septembre 2007
Messages : 951
Détails du profil
Informations forums :
Inscription : septembre 2007
Messages : 951
Points : 948
Points : 948
Tu peux faire un rmdir à la sortie de ta fonction deltree

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function del_tree ($path) {
  if (is_dir($path))
   {
     if ($dh = @opendir($path)) {
       while (($file = readdir($dh)) !== false) {
           if (($file != '.') && ($file != '..') && ($file != '...'))
                    if (is_dir($path . $file))
                    {
                    del_tree ($path . $file ."/");
                     rmdir ($path.$file);
                    }
                    else
                    {
                    unlink ($path . $file);
                    }
       }
       closedir($dh);
       }
    rmdir ($path);
   }
  elseif (is_file($path)) 
  {
    unlink ($path);
  }
  
}
batataw est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/09/2007, 01h09   #3
Invité de passage
 
Inscription : juillet 2006
Messages : 21
Détails du profil
Informations forums :
Inscription : juillet 2006
Messages : 21
Points : 3
Points : 3
Merci, mais ca n'a pas l'air de marcher
Peut être parceque le répertoire est ouvert par php.
J'avais pas pensé à le mettre, car il y avait déjà rmdir ($path); à la fin du fichier
pfrib est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/09/2007, 13h43   #4
Invité de passage
 
Inscription : juillet 2006
Messages : 21
Détails du profil
Informations forums :
Inscription : juillet 2006
Messages : 21
Points : 3
Points : 3
Oupsss...
Je viens de comprendre pourquoi celà ne marchait pas :
http://faq.assistancefree.fr/v1/faq/...xl&rac=354/348
Free.fr n'autorise pas l'utilisation de rmdir... :-(
pfrib est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/09/2007, 00h44   #5
Membre émérite
 
Inscription : septembre 2007
Messages : 951
Détails du profil
Informations forums :
Inscription : septembre 2007
Messages : 951
Points : 948
Points : 948
Les sites qui sont en safe_mod généralement autorise les commandes ftp, tu
peux faire un ftp_rmdir

http://www.php.net/manual/fr/function.ftp-rmdir.php

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
<?
    /**
     *    Delete the provided directory and all its contents from the FTP-server.
     *
     *    @param string $path            Path to the directory on the FTP-server relative to the current working directory
     */
    function DeleteDirRecursive ($resource, $path)
    {
        $list = ftp_nlist ($resource, $path);
        if ( empty($nlist) ){
            $list = RawlistToNlist ( ftp_rawlist($resource, $path), $path . ( substr($path, strlen($path) - 1, 1) == "/" ? "" : "/" ) );
        }
        if ($list[0] != $path){
            $path .= ( substr($path, strlen($path)-1, 1) == "/" ? "" : "/" );
            foreach ($list as $item){
                if ($item != $path.".." && $item != $path."."){
                    DeleteDirRecursive ($item);
                }
            }
            ftp_rmdir ($resource, $path);
        }
        else {
            ftp_delete ($resource, $path);
        }
    }
 
    /**
     *    Convert a result from ftp_rawlist() to a result of ftp_nlist()
     *
     *    @param array $rawlist        Result from ftp_rawlist();
     *    @param string $path            Path to the directory on the FTP-server relative to the current working directory
     *    @return array                An array with the paths of the files in the directory
     */
    function RawlistToNlist ($rawlist, $path)
    {
        $array = array();
        foreach ($rawlist as $item){
            $filename = trim(substr($item, 55, strlen($item) - 55));
            if ($filename != "." || $filename != ".."){
                $array[] = $path . $filename;
            }
        }
        return $array;
    }
?>
batataw est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 04h09.


 
 
 
 
Partenaires

Hébergement Web