lista file sottocartella elimina file
Buongiorno sto tentando di eliminare un file ho una pagina dove vedo il contenuto delle sottocartelle degli utenti il problema è che elimina tutti i file lasciando le sottocartelle vuote io vorrei che eliminasse solo il file cliccato ho fatto cosi:
Codice PHP:
lista file <?PHP
// Define a function to list files and subfolders
function list_files($dir) {
// Get an array of files and subfolders
$files = scandir($dir);
// Loop through the array
foreach ($files as $file) {
// Skip the current and parent directories
if ($file != "." && $file != "..") {
// Get the full path of the file or folder
$path = $dir . "/" . $file;
// Check if it is a file
if (is_file($path)) {
// Get the file name
$name = basename($path);
// Encode the file path
$del = unlink($path);
// Create a download link
echo "Documento: <a href='delfile.php?file=$del'>$name</a><br>";
}
// Check if it is a folder
if (is_dir($path)) {
// Print the folder name
echo "Cartella: <b>$file</b><br>";
// Call the function recursively
list_files($path);
}
}
}
}
// Call the function with the main directory
list_files("documenti/");
?>
--------------------
delfile.php
<?php
if(isset($_GET['file'])){
$del=$_GET['name'];
unlink($del) ;
}
?>
<?php
echo "<script> location.href='pagina.php'; </script>";
exit;
?>