Saving a text in Mysql

Asked

Viewed 78 times

0

Good afternoon to all,

I developed the code below to save the content of pdf files in the database.

As I could not do this straight from the pdf to the bank, I did the following process:

  • extract the text from a pdf and Gero a txt with the content;
  • turn the contents of this txt into string;
  • Save the contents in the bank.

Overall, the program is working, but not 100%. When I save the txt content through the html application I made, it saves in the database, only the last 3 words of the text.

Follow the code below:

<?php

set_time_limit(6000);

include_once ("conexao.php");

ini_set('default_charset', 'UTF8');

$file_tmp = $_FILES['file']['tmp_name'];
$file_fnl = $_FILES['file']['name'];

exec('tet --text '. $file_fnl);
$name = substr($file_fnl, 0, -4);
$file_txt = $name.'.txt';

$dados = file($file_txt);

foreach ($dados as $stringArray){

    $stringArrayF = ''.$stringArray;

    $DCM_nome = $file_fnl;
    $DCM_conteudo = $stringArrayF;  

    echo $DCM_conteudo; 
}

$result = "INSERT INTO test.conteudo_dcm (DCM_nome, DCM_conteudo) VALUES ('$DCM_nome','$DCM_conteudo');";

$_result = mysqli_query($conn,$result);

?>

To know if it would be a problem of the bank, I ran the line of the direct Insert in Workbench, but I changed the variable that saves the text, the text itself... and it worked. All contents saved in the bank.

With this, I believe (I’m almost sure) that the problem should be in my php.

If anyone can help me, I’d appreciate it!

  • exec('tet --text '. $file_fnl); What is that tet ago?

  • 1

    @Augustovasques It is a library that extracts the text from the pdf and generates a txt file with the content, among other features.

  • Have you done print_r($data) to check if the file is being generated and the data is correct? I will not present a solution before such verification.

  • 1

    Yes. The data is being generated correctly. I believe the problem is in the construction of foreach, but I have tried several ways and nothing.

1 answer

0

Exchange the foreach and its content by implode that joins elements of a matrix into a string.

<?php

set_time_limit(6000);

include_once ("conexao.php");

ini_set('default_charset', 'UTF8');

$file_tmp = $_FILES['file']['tmp_name'];
$file_fnl = $_FILES['file']['name'];

exec('tet --text '. $file_fnl);
$name = substr($file_fnl, 0, -4);
$file_txt = $name.'.txt';

$dados = file($file_txt);

$DCM_conteudo = implode(' ', $dados); // transforma o string array $dados numa string.

$DCM_nome = $file_fnl;

$result = "INSERT INTO test.conteudo_dcm (DCM_nome, DCM_conteudo) VALUES ('$DCM_nome','$DCM_conteudo');";

$_result = mysqli_query($conn,$result);

?>
  • Thanks for your help!!!!

Browser other questions tagged

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