Php delete files that are not on the server

0

I'm currently doing a php that launches a query, this query takes out the id of the user and the id of the documents, if the boards, you get the name of the folder, once this is done, another part of the php, take all the There are folders and put them in another array, once this is done, I try to get a 3 array that gets the folders that are not in the query of the first array, at the moment I have done all this, but there are folders that detect them and even so it does not erase them, or there are folders that are in the query and delete them etc etc ... I leave the php here.

<?php
include('../database/config.php');
//Consulta
    $query="SELECT id_usuario, id_documento FROM DOCUMENTOS";
//Fin de la consulta


$result = mysqli_query($conn,$query); //Lanza la consulta y la almacena en una variable

$row = mysqli_fetch_assoc($result); //Almacena en arrays los 

$carpeta[] = $row['id_usuario'].$row['id_documento']; //Con la combinacion de la columna id_usuario y id_documento se genera la carpeta y se almacena dentro la array

//Programa para sacar todos los directorios
$directorio = opendir(".");

while($archivo = readdir($directorio))
    {    
        if ((!is_file($archivo))and($archivo!='.')and($archivo!='..')) 
        {
            $documentos[]=$archivo;
        }
    }
closedir($directorio);  

$resultado = array_diff($documentos, $carpeta);
$numero = count($resultado);
for($i=0; $i<$numero; $i++){
    //unlink($resultado[$i]."/*");
    array_map('unlink', glob("$resultado[$i]/*"));
    rmdir($resultado[$i]);


}
print_r($resultado);
?>

Sql Attachment

CREATE TABLE 'DOCUMENTOS' (
  'id_usuario' varchar(45) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  'id_documento' varchar(45) NOT NULL,
  'nombre' varchar(45) NOT NULL,
  'formato' varchar(45) NOT NULL,
  'tiempo' datetime DEFAULT NULL,
  'minutos' int(11) DEFAULT NULL
) ;

EDIT 1

First of all it is necessary to do a while so that the variables are introduced inside the created array, now the problem is the unlink, I leave the first modification:

while($row = mysqli_fetch_assoc($result)) {
    $carpeta[] = $row['id_usuario'].$row['id_documento']; //Con la combinacion de la columna id_usuario y id_documento se genera la carpeta y se almacena dentro la array
}

2 Edit

I just modified the while a bit, first doing an array map and then doing the count of the total directories, by making a count of the difference, this could have a 2 values, but stored in position 1 and let's say another to 200, in this way, never reached position 200, I paste the changes and solve already post.

$resultado = array_diff($documentos, $carpeta);
$numero = count($documentos);
for($i=0; $i<$numero; $i++){
    //unlink($resultado[$i]."/*");
    array_map('unlink', glob("$resultado[$i]/*"));
    rmdir($resultado[$i]);
}
    
asked by Archeon El Centinela 15.05.2018 в 20:17
source

1 answer

0

According to what I could understand about your question, I based myself on the comment you made: First delete the file, then the folder , with this example you can unlink all the files within a directory and then delete that directory.

Use glob to search for all files that match a pattern.

function recursiveRemoveDirectory($directorio)
{
    foreach(glob("{$directorio}/*") as $file)
    {
        if(is_dir($file)) { 
            recursiveRemoveDirectory($file);
        } else {
            unlink($file);
        }
    }
    rmdir($directorio);
}
  

Documentation:

     
    
answered by 15.05.2018 в 20:24