Store name and file path uploaded in php

1

Gentlemen, cordial greetings to all.

I have a script to upload files to a folder on the server with php, initially I have the form that captures the file. If I include other information to the form, how could I store the name and path of the file in mysql and how could I retrieve it later in a list ?, to upload pdf files.

I thank you for concrete examples to understand how it works.

ARCHIVE 1: HTML FORM

<form action="upload.php" method="POST" enctype="multipart/form-data">       
   <h2>SUBIR ARCHIVOS</h2>      
   <input type="file" name="file">      
   <input type="submit" name="" value="Subir archivo">  
</form>

ARCHIVE 2: SCRIPT IN PHP

    <?php  

    $directorio = "subidas/"; $archivo = $directorio . basename($_FILES["file"]["name"]); 
    $tipoArchivo = strtolower(pathinfo($archivo, PATHINFO_EXTENSION));  
    if (move_uploaded_file($_FILES["file"] ["tmp_name"], $archivo)) {
        echo "archivo subido con exito";    
     } else {
        echo "error en la subida del archivo";
    }

    ?>

cordial greeting sr_luis

Try the code but I get an error, look:

File 1: INDEX.HTML

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <form action="upload.php" method="POST" enctype="multipart/form-data">
        <h2>SUBIR ARCHIVOS</h2>
        <input type="file" name="file">
        <input type="submit" name="" value="Subir archivo">
    </form>

</body>
</html>

FILE 2: Upload.php

<?php  

$directorio = "subidas/";

$archivo = $directorio . basename($_FILES["file"]["name"]);

$tipoArchivo = strtolower(pathinfo($archivo, PATHINFO_EXTENSION));

    if (move_uploaded_file($_FILES["file"] ["tmp_name"], $archivo)) {
        echo "archivo subido con exito";

    } else {
        echo "error en la subida del archivo";
    }

    require("conexion.php");

    $nombre = $_POST['file'];
    $ruta = "subidas/";
    $sql = "INSERT INTO archivos (nombre_archivo, ruta_subida) VALUES ('$nombre','$ruta')";
    $consulta = mysqli_query($conexion, $sql);

              if ($consulta==false) {
            echo "Error en la consulta";

          } else {


            echo "Datos almacenados exitosamente<br><br>";

          }

          mysqli_close($conexion);



?>

The connection file is good, because it effectively connects and stores the route, but it is not storing the name of the file.

The error that comes to me is: (!) Notice: Undefined index: file in C: \ wamp \ www \ upload-files \ upload.php on line 18

    
asked by Juan Carlos Monsalve 17.12.2018 в 00:24
source

1 answer

2

If the structure of the mysql table had as fields file_name and upload_path:

$nombre = $_POST['file'];
$ruta = "subidas/";
$conexion = mysqli_connect($servidor, $usuario, $contrasenya, $bd);
$sql = "INSERT INTO archivos (nombre_archivo, ruta_subida) VALUES ('$nombre', '$ruta')";
$consulta = mysql_query($sql, $conexion);
  

These lines add to a sql table the data of the file and the route.

To retrieve the list of files and their route:

$nombre = "nombre del archivo";
$conexion = mysqli_connect($servidor, $usuario, $contrasenya, $bd);
$sql = "SELECT * FROM archivos WHERE nombre_archivo == '$nombre'";
$consulta = mysql_query($sql, $conexion);
$fila = mysql_fetch_array($consulta);
while($fila) {
  echo "<tr>";
  echo "<td>{$fila["nombre_archivo"]}</td>";
  echo "<td>{$fila["ruta_subida"]}</td>";
  echo "</tr>";
  $fila = mysql_fetch_array($consulta);
}
    
answered by 17.12.2018 / 00:55
source