I need to convert a variable to an array, but I can't find the key. I hope to explain myself well. From a select multiple I receive a variable that brings for example the text adminUser banner files separated by a blank space. Through a foreach I try to extract the results to convert them into a single array as in this valid example:
array(3) { [0]=> string(11) "./adminUser" [1]=> string(10) "./archivos" [2]=> string(8) "./banner" }
The foreach is this
foreach ($directorioSelect as $excluidos) { $exclude_dir = array (preg_split("/ /", $excluidos)); }
But it throws me:
array(1) { [0]=> array(1) { [0]=> string(9) "adminUser" } } array(1) { [0]=> array(1) { [0]=> string(8) "archivos" } } array(1) { [0]=> array(1) { [0]=> string(6) "banner" } }
I'm trying with
explode(" ", $excluidos); y str_split($excluidos);
Can you help me and give a logical explanation of what I am doing wrong? Thanks in advance!!!
if (!$_POST) {
echo '<form action="" method="post">
Seleccionar directorios a excluir:<br>
<select multiple name="directorio[]" style="width:300px; height:200px; margin-top:10px;" required>';
// Primero lista directorios
$base = opendir("."); // Ruta base
while ($directorio = readdir($base)) {
if (is_dir($directorio)) {
if ($directorio!= "." && $directorio!= ".." && $directorio!= "backup") {
echo '<option value="'. $directorio . '">'. $directorio . '</option>';
}
}
}
closedir($base);
echo '</select><br>
<input type="submit" style="margin-top:10px;" value="Aceptar">
</form>';
} else {
$directorioSelect = $_POST["directorio"];
for ($i = 0; $i < count($directorioSelect); $i++) {
//echo "<b>" . $i . ":</b> " . $directorioSelect[$i] . "<br>"; // Imprime directorios a excluir seleccionados
}
$exclude_dir = [];
foreach ($directorioSelect as $excluidos) {
// Combina el anterior con el resultado de cada iteración
$exclude_dir = array_unique($exclude_dir);
$backup_files = '.'; // Ruta base
$backup_file_name = './backup/backups/copia de seguridad ' . date ("d-m-Y H-i") . '.zip'; // Ruta donde se crea el zip
$download_file = false; // True descarga el archivo una vez creado
if (!touch($backup_file_name)) {
echo "<script>alert('Permiso denegado en $backup_file_name')</script>";
exit();
}
}
class FlxZipArchive extends ZipArchive {
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
}
private function exclude($location) {
global $exclude_dir;
for($n = 0; $n < count($exclude_dir); $n++) {
if (strpos($location, $exclude_dir[$n]) === 0) {
return TRUE;
}
}
return FALSE;
}
private function addDirDo($location, $name) {
global $exclude_dir;
$name .= '/';
$location .= '/';
$dir = opendir ($location);
while ($file = readdir($dir)) {
if ($file == '.' || $file == '..') {
continue;
}
if ($this->exclude($location.$file)) {
continue;
}
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
}
}
$za = new FlxZipArchive;
$res = $za->open($backup_file_name, ZipArchive::CREATE);
if ($res === TRUE) {
$za->addDir($backup_files, basename($backup_files));
$za->close();
} else {
echo "<script>alert('No se ha creado la copia')</script>";
}
if ($download_file) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($backup_file_name) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($backup_file_name));
readfile($backup_file_name);
exit;
}
echo "<script>alert('Copia de seguridad de los archivos web, realizada correctamente')</script>";
echo "<meta http-equiv=\"refresh\" content=\"0;URL=" . $_SERVER['HTTP_REFERER'] . "#copia\">"; // Refresca la misma url
}
You have left over
array (
in$exclude_dir = array (preg_split("/ /", $excluidos));
because the function is going to return an array; also, it is not recommended to use preg_split when explode is enough, because the first one is for regular expressions and they are not needed in this case.Results:
Option 2 - All in the same arrangement
In case you need them all together and without repeating them:
Result: