Upload file, save MYSQL path and name

1

Save people!

I have the following question, or rather I do not know how to do it.

I have a resume form that is filled out and the user appends a .pdf file

Objective: user fills in the form name, email, telephone, etc ... and attach the curriculum in pdf.

This pdf file should be renamed to the id-name filled out on the form, and uploaded to the files folder.

And be recorded in the mysql database, all form data, and so the file name and its path.

After this has administrative panel that lists the resumes and should show the name and path of the file to download by the administrative panel.

I searched the net and ended up getting some script parts and tried to mount, but it's not rolling.

I need the help of the staff here, thank you for helping.

Explaining what code I have, and I have managed to resolve it so far, the system at least localhost already sends the file to the specified folder, but not the INSERT in the database of any information.

I have HTML:

<html>
 <head>
  <title>Upload de arquivos</title>
 </head>
   <body>
      <form class="form-horizontal" method="POST" action="upload.php" enctype="multipart/form-data">

        <label>titulo: </label><br />

           <input type="text" id="titulo" name="titulo" /><br /><br />

              <label>Selecione o arquivo: <br />

                   <input type="file" name="arquivo" id="arquivo" size="45" /></label> <br />

                      <input type="submit" id="btnEnviar" value="Enviar Arquivo" /><br />

                           <input type="hidden" id="arquivo_id" name="arquivo_id" />

     </form>

  </body>

I have the SQL table file and three columns ID - TITLE - FILE

CREATE TABLE 'arquivo' (
  'id' int(11) NOT NULL,
  'titulo' varchar(255) NOT NULL,
  'arquivo' varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now PHP:

<?php
$nome_temporario=$_FILES["arquivo"]["tmp_name"];
$nome_real=$_FILES["arquivo"]["name"];
copy($nome_temporario,"imagens/$nome_real");

$servidor = "localhost";
    $usuario = "root";
    $senha = "";
    $dbname = "curriculo";

    //Criar a conexão
    $conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
    if(!$conn){
        die("Falha na conexao: " . mysqli_connect_error());
    }else{
        //echo "Conexao realizada com sucesso";
    }

// Lendo os campos do .html
$arquivo_id = $_POST['arquivo_id'];
$titulo = $_POST['titulo'];
$arquivo = $_FILES['arquivo']["name"];
// ~~~~~~~~~~~~~~~~~~~~~~~~~~

// Codigo de inserção
$sql = "INSERT INTO arquivo (titulo, arquivo_id) VALUES ('$titulo', 'imagens/$arquivo')";

// Converte e Executa a query
$inserir = mysqli_query($conn, $sql);


// Resultado para o .html
if(mysqli_affected_rows($conn) != 0){
                echo "
                    <META HTTP-EQUIV=REFRESH CONTENT = '60;URL=http://localhost/loja/adm/administrativo.php?link=14'>
                    <script type=\"text/javascript\">
                        alert(\"O arquivo foi enviado com sucesso.\");
                    </script>
                ";  
                }else{
                    echo "
                        <META HTTP-EQUIV=REFRESH CONTENT = '60;URL=http://localhost/loja/adm/administrativo.php?link=14'>
                        <script type=\"text/javascript\">
                            alert(\"Arquivo não foi enviado.\");
                        </script>
                    ";  
                }
// Exibe dados sobre o erro:

?>

To be well lost in the following. hahahaha

Anyone who wants to help here.

I await and thank you

    
asked by anonymous 31.03.2017 / 19:47

1 answer

1

Begin by modifying the arquivo table with the following command:

  

alter table file modify id int (11) not null auto_increment;

With this you add the auto_increment attribute to the table.

In a query SQL , you always have to escape the data that you are going to insert, using double quotation marks " or braces {} .

$caminho = "imagens/" . $arquivo; 

"INSERT INTO arquivo (titulo, arquivo_id) VALUES ('{$titulo}', '{$caminho}')"
ou
"INSERT INTO arquivo (titulo, arquivo_id) VALUES ('" . $titulo . "', '". $caminho . "')"

To generate unique and random names, you can use the uniqid function:

$caminho = "imagens/" . uniqid('ficheiro_') ; 

There is no guarantee that this function will always return random values, so you can use functions that are appropriate for this, see this answer here .

    
31.03.2017 / 23:01