Form to upload a document to my server

3

I'm starting with php and I'm already acquiring the basic knowledge, but I just faced a wall when trying to make a form with a field to upload a document and other input to put a number, and that when uploading the document, be renown with the number that I have assigned in the input. I know I need code to put, but I do not know how to do it.

I have this piece of code at this time

<form enctype="multipart/form-data" action="uploader.php" method="POST">
              <input name="uploadedfile" type="file"/>
              <input type="number" placeholder="Nº de Albarán">
              <input type="submit" value="SUBIR Y COMPLETAR AVISO"/>
</form>

And here the function code

<?php 

function dbConnect (){
 	$conn =	null;
 	$host = 'Localhost';
 	$db = 	'db_name';
 	$user = 'db_user';
 	$pwd = 	'db_pass';
	try {
	   	$conn = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pwd);

	}
	catch (PDOException $e) {
		echo '<p>Error al conectar a la base de datos</p>';
	    exit;
	}
	return $conn;
 }


$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
 if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
 	echo "El archivo ". basename( $_FILES['uploadedfile']['name']). " ha sido subido";
} 
 else{
	echo "Ha ocurrido un error";
}
 ?>
    
asked by Alejandro 05.01.2018 в 13:45
source

1 answer

2

First you fix your input you have, you must put "name" and you should always put a "id" as it will facilitate many processes performed with javascript and php. Knowing this, you change your input:

<input type="number" placeholder="Nº de Albarán">

By:

<input id="number" name="number" type="number" placeholder="Nº de Albarán">

So that your html will be in the following way (I have placed "name" and "id" to each element remember to always do this for <form> and the <input> ):

<form id="carga_archivo" name="carga_archivo" enctype="multipart/form-data" action="uploader.php" method="POST">
              <input id="uploadedfile" name="uploadedfile" type="file"/>
              <input id="number" name="number" type="number" placeholder="Nº de Albarán">
              <input id="enviar" name="enviar" type="submit" value="SUBIR Y COMPLETAR AVISO"/>
</form>

You get the number by post:

$nombre_archivo=$_POST['number'];

You should know that format is handling the files (which files you will only receive). If you will only receive ". Txt" for example, you must modify the file name as:

$nombre_archivo= $_POST['number'].'txt';

If it's pdf then:

$nombre_archivo= $_POST['number'].'pdf';

If you do not decide on a particular format, you get the format type of $ _ FILES . So:

$tipo_archivo = $_FILES['uploadedfile']['type'];

$tipo_archivo = explode("/", $tipo_archivo);
$tipo_archivo= '.'.$tipo_archivo[1];

$nombre_archivo= $_POST['number'].$tipo_archivo;

This line of code that you have would not go anymore:

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

But you would go these:

$target_path = "uploads/";
$nombre_archivo= $_POST['number'].'pdf'; // según si es pdf o txt u otro
$target_path = $target_path.$nombre_archivo;

That way if your input "number" sends the value "0023" for example, taking into account your route then it would look like this:

$target_path = 'uploads/0023.pdf';

and when you load it, it will have the name you need. I put your complete php code but with the corrections already made:

 <?php 

    function dbConnect (){
        $conn = null;
        $host = 'Localhost';
        $db =   'db_name';
        $user = 'db_user';
        $pwd =  'db_pass';
        try {
            $conn = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pwd);

        }
        catch (PDOException $e) {
            echo '<p>Error al conectar a la base de datos</p>';
            exit;
        }
        return $conn;
     }


    $target_path = "uploads/";
    $nombre_archivo= $_POST['number'].'pdf'; // recuerda cambiar esto por el 
                                             //formato de archivo que 
                                             // quieras... 'jpg', 'txt', etc
    $target_path = $target_path.$nombre_archivo;

     if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
        echo "El archivo ".$nombre_archivo. " ha sido subido";
    } 
     else{
        echo "Ha ocurrido un error";
    }
     ?>

That's it. A greeting

    
answered by 05.01.2018 / 14:58
source