-1

the php file and the folder are in the same directory, when I check the path that includes a subfolder and a file it shows that the file does not exist. but the path if it is in the windows explorer if it exists.

<?php

include('conexion.php');
if (!empty($_GET['copia']) ){
    $consultar1 = mysqli_query($conexion,"SELECT * FROM documentos" );
    $total = mysqli_num_rows($consultar1);
    if($total > 0){
        if (!file_exists("copia_seguridad")) {
                mkdir("copia_seguridad",0777,true);                 
            }
            
        while ( $doct = mysqli_fetch_row($consultar1)){
            $consultar_categoria = mysqli_query($conexion,"SELECT nombreCategoria FROM categorias");
            $subdirectory = mysqli_fetch_array($consultar_categoria);
            $longitud = count($subdirectory);
            for($i=0; $i<$longitud; $i++){
            $nombre_fichero = 'documentos/$subdirectory[$i]/".$doct[1].';
            if (file_exists($nombre_fichero)) {
               echo "El fichero $nombre_fichero existe";
             } else {
               echo "El fichero $nombre_fichero no existe";
              }

            }
        }
        echo  "<script> alert('Copia de Seguridad realizada...'); </script>";
        echo "<script>document.location.href = 'admin.php'; </script>";
    }else{
        echo  "<script> alert('No hay documentos para guardar...'); </script>";
        echo "<script>document.location.href = 'admin.php'; </script>";
    }
}    
?>

windows explorer path

error with "

error with '

change the double quotes in the path to a single quote but it still doesn't detect the file.

How should the path be built for php to detect subfolders and files?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    First you need to understand how to read error messages AND how single and double quotes work. Which, if you read your error messages, will tell you. – TimBrownlaw Oct 17 '20 at 22:46
  • Does this answer your question? [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – user3783243 Oct 17 '20 at 22:47
  • can you try `$nombre_fichero = "documentos/".$subdirectory[$i]."/".$doct[1];` ? – Marcello Perri Oct 17 '20 at 23:05
  • Also you should use the DIRECTORY_SEPARATOR constant to speed up lookup across Windows and Unix. You may have notice the slashes in different directions. This can cause unexpected behavior. https://stackoverflow.com/questions/26881333/when-to-use-directory-separator-in-php-code – Richard Tyler Miles Oct 17 '20 at 23:13
  • Undefined offset occurs when I try to access an element of the array that does not exist, how do I save the result of a query in an array? – Ives Rodriguez Oct 17 '20 at 23:26
  • @IvesRodriguez Can you, just before the line for($i=0; $i<$longitud; $i++){ - add in var_dump($doct); and var_dump($subdirectory); and var_dump($longitud); and show the results for those. – TimBrownlaw Oct 17 '20 at 23:38
  • just save one value https://i.imgur.com/xITSpjx.jpg, it should save five values https://i.imgur.com/AUyprMU.jpg – Ives Rodriguez Oct 17 '20 at 23:46

1 Answers1

-1

Try to change

$nombre_fichero = 'documentos/$subdirectory[$i]/".$doct[1].';

to

$nombre_fichero = 'documentos/' . $subdirectory[$i] . '/' . $doct[1].';

Also, try to avoid using other languages in variable "names".

Mootje
  • 127
  • 9