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