Choose path where upload file to PHP server

3

Good morning, I want to upload a file to a server using PHP. I already achieved it, the issue is that I want to generate a button to be able to choose the directory where that file will be placed.

Can you help me?

The code that I currently have is:

<?php
$archivo = (isset($_FILES['archivo'])) ? $_FILES['archivo'] : null;
if ($archivo) {
   $extension = pathinfo($archivo['name'], PATHINFO_EXTENSION);
   $extension = strtolower($extension);
   $extension_correcta = ($extension == 'jpg' or $extension == 'jpeg' or $extension == 'gif' or $extension == 'png' or $extension == 'svg');
   if ($extension_correcta) {
      $ruta_destino_archivo = "img/{$archivo['name']}";
      $archivo_ok = move_uploaded_file($archivo['tmp_name'], $ruta_destino_archivo);
   }
}
?>
<!DOCTYPE html>
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title> Subir archivos </title>
 </head>
 <body>
    <?php if (isset($archivo)): ?>
       <?php if (!$extension_correcta): ?>
         <span style="color: #f00;"> La extensión es incorrecta, el archivo debe ser jpg, jpeg, gif o png. </span>
       <?php elseif (!$archivo_ok): ?>
         <span style="color: #f00;"> Error al intentar subir el archivo. </span>
      <?php else: ?>
         <strong> El archivo ha sido subido correctamente. </strong>
         <br />
         <img src="archivos/<?php echo $archivo['name'] ?>" alt="" />
      <?php endif ?>
   <?php endif; ?>
   <form method="post" action="subir-archivo.php" enctype="multipart/form-data">
      <label> Archivo </label>
      <input type="file" name="archivo" required="required" />
      <input type="submit" value="Subir" />
   </form>
 </body>
</html>

I want to be able to choose the directory $ destination_path_file.

Can someone help me? Thanks

    
asked by dtorralba 03.01.2017 в 16:07
source

2 answers

0

You can use an input where you put the name of the file or simply using checkbox, so you make sure you do not write a malicious file name.

<?php
$archivo = (isset($_FILES['archivo'])) ? $_FILES['archivo'] : null;
$ruta_destino_archivo = null;
if ($archivo) {
   $extension = pathinfo($archivo['name'], PATHINFO_EXTENSION);
   $extension = strtolower($extension);
   $extension_correcta = ($extension == 'jpg' or $extension == 'jpeg' or $extension == 'gif' or $extension == 'png' or $extension == 'svg');
   if ($extension_correcta) {
      if(isset($_POST["directorio"])){
         if($_POST["directorio"] == 0){
            $ruta_destino_archivo = "img/{$archivo['name']}";
         }else if($_POST["directorio"] == 1){
            $ruta_destino_archivo = "css/{$archivo['name']}";
         }else if($_POST["directorio"] == 2){
            $ruta_destino_archivo = "js/{$archivo['name']}";
         }
      }else{
          $ruta_destino_archivo = "img/{$archivo['name']}";
      }
      $archivo_ok = move_uploaded_file($archivo['tmp_name'], $ruta_destino_archivo);
   }
}
?>
<!DOCTYPE html>
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title> Subir archivos </title>
 </head>
 <body>
    <?php if (isset($archivo)): ?>
       <?php if (!$extension_correcta): ?>
         <span style="color: #f00;"> La extensión es incorrecta, el archivo debe ser jpg, jpeg, gif o png. </span>
       <?php elseif (!$archivo_ok): ?>
         <span style="color: #f00;"> Error al intentar subir el archivo. </span>
      <?php else: ?>
         <strong> El archivo ha sido subido correctamente. </strong>
         <br />
         <img src="archivos/<?php echo $archivo['name'] ?>" alt="" />
      <?php endif ?>
   <?php endif; ?>
   <form method="post" action="subir-archivo.php" enctype="multipart/form-data">
      <label> Archivo </label>
      <input type="file" name="archivo" required="required" />
      <label> img/nombre_archivo </label>
      <input type="checkbox" name="directorio" value="0" required="required" />
      <label> css/nombre_archivo </label>
      <input type="checkbox" name="directorio" value="1" required="required" />
      <label> js/nombre_archivo </label>
      <input type="checkbox" name="directorio" value="2" required="required" />

      <input type="submit" value="Subir" />
   </form>
 </body>
</html>

You can do it this way, or by placing an input text that allows you to obtain the directory you want, but you have to be careful with security issues, because if you place a foreign directory of the type / etc / / var / o / home / can generate a security breach.

    
answered by 03.01.2017 в 16:18
0

An option would be in a select to load the directories with scandir and already with that selection you can build the path in your php. and to better treat the routes you can check this.

link

link

    
answered by 03.01.2017 в 22:50