Assign two variables to a path in PHP _FILES

0

I have the following code to save files in php, it works correctly, I create the route with the corresponding id well

        $permitidos = array("application/pdf");     
        $limite_kb = 8360;

        if(in_array($_FILES["acta"]["type"], $permitidos) && $_FILES["acta"]["size"] <= $limite_kb * 1024){

          $ruta = '../../files/personas/proyectos/actas/'.$id_persona.'/';
          $acta = $ruta.$_FILES["acta"]["name"];

          if(!file_exists($ruta)){

              mkdir($ruta);
          }

          if(!file_exists($acta)){

              $resultado1 = @move_uploaded_file($_FILES["acta"]["tmp_name"], $acta);



          } else{

              $actaErr= "El archivo ya existe";
              $valid = false;
          }
        } else{
          $actaErr= "Formato no valido o el archivo sobrepasa el tamaño permitido. Solo se aceptan archivos PDF y el tamaño máximo que puede enviar es de 8 MB";
          $valid = false;
        }

      }else{
          $actaErr= "Ups! El acta de asamblea es obligatoria";
          $valid = false;
      }

But I want to add one more folder after $id_persona with $id_proyecto I tried like this but it sends me the error Warning: mkdir(): No such file or directory

 $ruta = '../../files/personas/proyectos/actas_asamblea/'.$id_persona.'/'.$id_proyecto.'/';
    
asked by IndiraRivas 28.11.2018 в 21:08
source

1 answer

1

Let's give an example where $id_persona does not exist. How do you access id_proyecto ?

You will have to check first if there is the path $id_persona to then create $id_proyecto

$ruta = '../../files/personas/proyectos/actas/'.$id_persona.'/';

if(!file_exists($ruta)){
   mkdir($ruta);
}
$ruta = '../../files/personas/proyectos/actas/'.$id_persona.'/'.$id_proyecto.'';

if(!file_exists($ruta)){
   mkdir($ruta);
}

$acta = $ruta.$_FILES["acta"]["name"];

In this way, you first check if the $id_persona directory exists and if it does not exist, create it, then check $id_proyecto and if it does not exist, create it.

    
answered by 29.11.2018 / 00:03
source