Save image name and extension and upload to a specific php folder and then show in a table

Asked

Viewed 683 times

0

When showing a query in a table I create these three fields for the user to update to the line showing the query data:

<?php
$tabela1 .= '<td> <input type="file" name= "Imagem['.$rows_cursos['Id'].']" value="'.$rows_cursos['Imagem'].'"></td>';

$tabela1 .= '<td> <input type="text" name= "Tratamento['.$rows_cursos['Id'].']" value="'.$rows_cursos['Tratamento'].'"></td>';

$tabela1 .= '<td> <input type="radio" name= "Id['.$rows_cursos['Id'].']" value="Pendente"> Pendente  <input type="radio" name= "Id['.$rows_cursos['Id'].']" value="Concluido">Concluido</td>';
?>

In this next step I keep in this way the image path, but it does not guard the complete path, it only keeps the image name and the format "DSCF2712.JPG":

    <?php  
    if(isset($_POST['registar']))
    {
    $servername = "xxx.xxx.x.xx";
    $username = "xxxxx";
    $password = "xxxxxxx";
    $dbname = "xxxxxxxx";

    $conn = new mysqli($servername, $username, $password, $dbname);
    $conn->set_charset('utf8');

    $registro = $_POST['Id'];
    $imagem = $_POST['Imagem'];
    $tratamento = $_POST['Tratamento'];


    foreach($registro as $Id => $estado) { 


        $conn->query("UPDATE RegistoManutencao SET Estado='$registro[$Id]', Imagem = '$imagem[$Id]', Tratamento = '$tratamento[$Id]' WHERE Id='".$Id."'"); 
    } 


    }
    ?>

I’ve been researching and what I realized is ideal is just to keep the name of the image and the extension, as I have, but now how do I show it? I’m doing it this way:

<?php
$result_cursos = "SELECT centrodb.RegistoManutencao.Imagem FROM centrodb.RegistoManutencao";

$resultado_cursos = mysqli_query($conn, $result_cursos);

$tabela1 .= '<div style="float: center" table align="center">';

$tabela1 .= '<table border="5">';

$tabela1 .= '<tr>';

$tabela1 .='<thead>';

$tabela1 .= '<tr>';

$tabela1 .= '<th>Imagem</th>';

$tabela1 .= '</tr>';

$tabela1 .='</thead>'; 

$tabela1 .='<tbody>';

    while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {   

$tabela1 .= '<tr>';

$tabela1 .= '<td><img src="' .$rows_cursos['Imagem']. '" /></td>';

$tabela1 .= '</tr>'; 
}
$tabela1 .= '</tr>';

$tabela1 .='</tbody>'; 

$tabela1 .= '</table>';

$tabela1 .= '</div>';

echo $tabela1;

?>

And the result is this, does not show the image, but if you do inspect it shows the name and the extension that is in the database:

inserir a descrição da imagem aqui

I think the problem is not finding the complete path of the image

  • inspect the element and see which URL is being generated

  • I don’t get it, you can be more specific?

  • Press the right mouse button on the image and go on to inspect. Are you sure that what is saving in the bank are the bases 64, JPEG?

  • I know they are jpeg, but in my case it can also be png, I needed to show all the images inserted in the database in data type LONGBLOB on the chart, but I’m not getting

  • Tell me what kind of column you’re saving the image URL.

  • currently varchar to save image name and extension.

  • i besides saving the image name and the extension in the database table, should put a button to upload the images always to a specific folder of the server. I am using the Ubuntu server 16.04 and sharing folders with samba

  • I understand, but how can you save a field URL input type="file" ?

  • So instead of saving the url, just save the name and extension of the image in the database table and save it in a specific folder inside the server, where the correct one should put the image there with an upload and then show the image in a table

Show 4 more comments

2 answers

0


In PHP

$registro = $_POST['Id'];

$tratamento = $_POST['Tratamento'];

//Pasta onde as imagens serão salvas
$pasta = "img";
//se não existir será criada
if (!file_exists($pasta)){
    mkdir("$pasta", 0777);
}

$diretorio = "img/";

    foreach ($registro as $Id => $estado) {

        $url = $diretorio .  $_FILES['Imagem']['name'][$Id];

        $nome_arquivo = $_FILES['Imagem']['name'][$Id];

        // salva as imagens na pasta
        move_uploaded_file($_FILES['Imagem']['tmp_name'][$Id], $url);

        // insere o nome com extensão no banco de dados
        $conn->query("UPDATE RegistoDiario SET Estado='$registro[$Id]', Imagem = '$nome_arquivo', Tratamento = '$tratamento[$Id]' WHERE Id='".$Id."'");

    }

In HTML

In the form tag you need to set the enctype to "Multipart/form-data" which indicates that the form we are using will work with sending files

echo "<form method='POST' action='' enctype='multipart/form-data'>";

$tabela1 .= '<td><img src="img/' .$rows_cursos['Imagem']. '" /></td>';

0

The path of the image is missing,

$tabela1 .= '<td><img src="' .$rows_cursos['Imagem']. '" /></td>';

this would only work if your image is in the same file folder if you use an image folder structure for example that is next to the file ve should do:

$tabela1 .= '<td><img src="/imagens/'.$rows_cursos['Imagem']. '" /></td>';
  • but I don’t always upload images from the same folder, it depends on the image.... works in the same way? Or before I insert them into the database I have to put them in a specific folder?

  • The same result appeared with the solution you presented. I have to save the images in a specific folder?

  • The default is to use a specific folder for images pq facilitates addressing, maintenance and addressing.

  • But it has to be on the server or it can be on the user’s computer?

  • Initially on the server, since php only runs on the server is not on the client.

  • You can place an example to upload to a specific folder within the server and at the same time save the name and extension of the same image to the database table?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.