Support the ongoing development of Laravel.io →
posted 3 months ago
Laravel
Last updated 3 months ago.
0

Hi @layito , Can you explain your problem so that we can help you better.

Do you want to save the contents of the file in a database table or upload the file and save the filename instead?

1

Hello, so my question was how to save the contents of a file in a database table

0

So you want to read the contents of the file and store them in the database table. Is that right?

1

yes exaclty that's i want

0

Isn't it better to upload the file to the storage and store only the path of the file in the database table column?

1

No maybe in another situation , in my case i need the data that's the most important.

0

Do you need to store the data from a <file_name>.txt in a database?

// this is the data in my <file_name>.txt
accepting	1.6	    0.66332	 [2, 2, 2, 1, 1, 2, 1, 3, 1, 1]
accepts	1.3	      0.45826	[1, 2, 1, 1, 1, 2, 2, 1, 1, 1]
accident	-2.1	      0.83066	[-2, -2,  -1, -3, -4, -2, -2, -1, -2, -2]
accidental	-0.3	   0.45826	[-1, -1, 0, 0, 0, 0, 0, 0, -1, 0]
accidentally	-1.4	 0.91652	[-2, 0, -2, 0, -3, -1, -1, -1, -2, -2]
accidents	-1.3	    0.78102	[-1, -1, -1, -1, -2, 0, -3, -1, -2, -1]
accomplish	1.8	   0.6	[1, 2, 3, 2, 2, 2, 1, 1, 2, 2]

if you need only first and second col you can just write code like this:

<?php
$fileName = "filename.txt";

public function make_lex_dict()
{

     $lex_dict = [];
     $fp = fopen($fileName, "r");
     if (!$fp) {
         die("Cannot load file");
     }

     while (($line = fgets($fp, 4096)) !== false) {
         list($word, $measure) = explode("\t", trim($line));
         $lex_dict[] = [$word,$measure]
     }

     return $lex_dict;
 }
 print_r(make_lex_dict());
Last updated 3 months ago.
1
Solution selected by @layito

Thanks dear, it was helpful.

i dont't know if you can help me to do the to do the inverse, export the data of a table to a file txt

0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

Layito layito Joined 17 Aug 2022

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.