-2

I'm using a txt file as a database for a project. I read it, convert it to an array with a delimiter, and process the arrays (as id, title, text..)

But now, i will have to add large chunks of text in my text database, and i'm a litle concerned, that in time, my file will become large, and i will have memory issues, and my server provider won't be happy :)

So far, my idea is to split my database to two files: one with the id and the text, and a second with the id and the title - and the line that the first line was saved:

first file:

id|text
15|Lorem ipsum dolor sit amet, consectetur adipiscing elit...

second file:

id|title|line
15|Lorem ipsum|2

..and to find a way to read the first (large) file without parsing it to arrays, just go to the line i get from the second file, and then parse it to arrays to use it with my code.

My two problems:

One, i didn't find out (yet) how to get a line number in return when i append a line to a text file. One idea is to read again the file, and count it's lines, but i thing this will not help my with my memory leak fears :)

Two, even if i have the line number, my option is to read the big file line by line, and stop when the integer stops at my line. Is this the best way i can do it?

Thanks for your time, i hope my poor english and PHP knowledge made any sense :)

--

TL;DR: i want to parse a large txt file, and looking for help to a) know the line when i append something to it, and b) to go and read that line, without reading all the file.

anothermh
  • 9,815
  • 3
  • 33
  • 52
Giannis
  • 49
  • 6
  • Your question is too long, without any code. No one will help you. – Vincent Decaux Nov 25 '17 at 20:18
  • If the file is big enough that you may have problems reading it you can use [a generator](https://scotch.io/tutorials/understanding-php-generators). – Matthew Daly Nov 25 '17 at 20:23
  • 1
    I do not recommend text files if you depend on the data and if you have lots of visitors. PHP can't (as far as I know, and own tests have show) have two users open the same file at the same time. The first user will open the file, for the second user PHP will not be able to open the file and because of that it believes it does not exist. So it creates the file and all your data is lost. – Andreas Nov 25 '17 at 20:23
  • As english is not my native language, i'm trying my best to explain the best way i can my problem :) I understand its big, and people won't have the time to read everything, though, so you have good point. – Giannis Nov 25 '17 at 20:24
  • But I agree with Andreas. This is a job for a proper database. SQLite will work with a single file and if it doesn't have many users it might be suitable. – Matthew Daly Nov 25 '17 at 20:24
  • Andreas, one moderator, and not many readers. My problem is mostly my data and the memory needed to parse them, than my traffic :) – Giannis Nov 25 '17 at 20:25
  • Still you only need two users to f it up. If you insist on text files. At least keep a copy of the file(s) at all times. – Andreas Nov 25 '17 at 20:28
  • Two users - and some bad timing :) but yes, i understaind the problem. – Giannis Nov 25 '17 at 20:29
  • Best to just listen to @Andreas this is the best advice that you are going to receive. – mickmackusa Nov 26 '17 at 03:57
  • I'm reading and trying SQLite as we speak, as Matthew and Andreas suggested :) I've even found [PHPDesktop](https://github.com/cztomczak/phpdesktop) to work with :) But, i still have to finish my project so the large txt file is something i must complete - one way, or another. – Giannis Nov 26 '17 at 15:13

1 Answers1

0

Take a look at the fgetcsv http://php.net/manual/en/function.fgetcsv.php

fgetcsv — Gets line from file pointer and parse for CSV fields

array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\" ]]]] )

Set delimiter = "|", enclosure and escape to ""