move_uploaded_file does not upload the selected files [closed]

1

Good morning classmates, I have problems when uploading files, I'm doing tests in local, the data is sent to the bbdd, I generate the destination path, but at the time of executing the function move_uploaded_file does not upload them, and does not throw any error.

The error tells me that the destination route is not a directory, since I gave permission to the parent directory.

$ruta_archivo = rutaBase.'documentos/otros';
$ruta_final = $ruta_archivo."/".$nombre_archivo;
if( is_dir($ruta_final) ){
            if( is_writable($ruta_final) ){
                if( move_uploaded_file($direccion_temporal, $ruta_final) ){
                    $respuesta['status'] = '1';
                    $respuesta['ruta'] = $ruta_final;
                    $respuesta['tmp'] = $direccion_temporal;
                }else{
                    $respuesta['status'] = '0';
                    $respuesta['ruta'] = $ruta_final;
                    $respuesta['tmp'] = $direccion_temporal;
                }
            }else{
                $respuesta['estado'] = 'la ruta no tiene permisos de escritura';
                $respuesta['status'] = '0';
            }
        }else{
            $respuesta['estado'] = 'la ruta no es directorio';
            $respuesta['status'] = '0';
            $respuesta['ruta'] = $ruta_final;
        }
    
asked by Hadik Chivo Chavez 06.07.2018 в 17:47
source

1 answer

1

The problem is that you are asking if the file is a directory in your logic, here: if( is_dir($ruta_final) ){ , because what you stored in $ruta_final was a reference to the file itself: $ruta_final = $ruta_archivo."/".$nombre_archivo; .

We could say that you have missed the naming convention and that has led to confusion in your code.

If you change this: if( is_dir($ruta_final) ){ for this: if( is_dir($ruta_archivo) ){ should work.

Here I propose this, applying a clearer naming convention :

$ruta_archivo = rutaBase.'documentos/otros';
$el_archivo = $ruta_archivo."/".$nombre_archivo;
if( is_dir($ruta_archivo) ){
            if( is_writable($el_archivo) ){
                if( move_uploaded_file($direccion_temporal, $el_archivo) ){
                    $respuesta['status'] = '1';
                    $respuesta['ruta'] = $el_archivo;
                    $respuesta['tmp'] = $direccion_temporal;
                }else{
                    $respuesta['status'] = '0';
                    $respuesta['ruta'] = $el_archivo;
                    $respuesta['tmp'] = $direccion_temporal;
                }
            }else{
                $respuesta['estado'] = 'la ruta no tiene permisos de escritura'; //¿La ruta o el archivo?
                $respuesta['status'] = '0';
            }
        }else{
            $respuesta['estado'] = 'la ruta no es directorio';
            $respuesta['status'] = '0';
            $respuesta['ruta'] = $el_archivo;
        }

PD :

There is another confusing element in the ruta of the array $respuesta . It seems that this key would refer to the file itself, not to a route. Maybe you should change it too. The badly applied naming convention problem leads you to confusion and if another programmer reads your code, it becomes very difficult to understand.

    
answered by 07.07.2018 / 02:00
source