Problem reading from file

Problem reading from file

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

i have one text box, and when i write in it, everything is written to the file "text.txt", and after every new sentence to go on a new line - "\r\n".

  $file = fopen("text.txt", 'a') or die("can't open file");              fputs($file, $_POST['text']."\r\n");               fclose($file);

the problem is when i open the file through a "textarea box" it gives me just the first line.

<textarea rows="20" cols="81"> <?php $fp = fopen("text.txt", "r"); $line = fgets($fp); echo $line; ?> </textarea>

Can be the problem because of the carriage return and line feed?



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • igor86  Male
  • Stručni saradnik
    Web programiranje
  • Pridružio: 24 Maj 2006
  • Poruke: 1633

ovo

$fp = fopen("text.txt", "r"); $line = fgets($fp);

zamjeni sa

$line = file_get_contents("text.txt");



offline
  • Blood  Male
  • Ugledni građanin
  • Pridružio: 26 Jul 2003
  • Poruke: 384
  • Gde živiš: Beograd

try to read the whole file Smile
while (!feof($fp)) {   $contents .= fread($fp);   // or print line by line   // $contents = fread($fp);   // echo $contents; } fclose($fp); echo $contents;

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

OK.i tried igor86's example and it's working, next problem is how can i make, 1.every time i write a sentence in the file, to give me in the beggining of the file and not to delete the other content,
OR
2.when i write 30 lines and the textbox is 20 lines , the pointer to be on the last line, because now every time when i write something new i must to slide down to the last line to see the new wroted line.

P.S I m little drunk and if you dont understand me, i will explain you better.Smile

offline
  • igor86  Male
  • Stručni saradnik
    Web programiranje
  • Pridružio: 24 Maj 2006
  • Poruke: 1633

ovo
  $file = fopen("text.txt", 'a') or die("can't open file");              fputs($file, $_POST['text']."\r\n");               fclose($file);

zamjeni sa

if( !file_put_contents('text.txt', ( $_POST['text'] ."\r\n". file_get_contents('text.txt'))))    die("can't open file");

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

igor86 ::
if( !file_put_contents('text.txt', ( $_POST['text'] ."\r\n". file_get_contents('text.txt'))))    die("can't open file");


OK, this is for the option 1 and it's working,but the second option is better.
Is there any chance to make the slide always to be at the bottom?

Another question that is not related with the other posts.
How can a redirect a page with 4 frames to a new page in the same window (not new tab or window).

Ko je trenutno na forumu
 

Ukupno su 1204 korisnika na forumu :: 107 registrovanih, 15 sakrivenih i 1082 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 6018 - dana 19 Dec 2025 13:41

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: 100jan, A.R.Chafee.Jr., AC-DC, alberto, ArmFPGA, Asparagus, Aster Blistok, b07, bax0904, bojan581, borya90, Branko Matić, brkan1, BWG, Cicumile, Cirkon, Citalac, crazydkure, DavidA, dd11ll, debeli, dekan.m, Demi87, Denaya, desmeki, Df410, dok80, doktor1964, Donneraj, Dovla, Draganeli, draganl, DrMrPr, Duh sa sekirom, dule10savic, dushan, Dusko Nikolin, ElGenius, Ercomero, FileFinder, Gall, Gitzherai, Hardenberg, Henry, istina, Jager715510, Jan, jeen yuhs, Jose, Karla, klepesina, Kobrim, Koce, kovinacc, Krusarac, Kubovac, ladro, Litostroton, ljubo70, Ljusa, MarijaC84, markolopin, mexo, Mickey91, Miki281, mikrimaus, Miler88, mkukoleca, nebidrag, nelezele, nevjerna beba, Nobunaga, nuki1234, Pekman, PITT, raketaš, rovac, sales, samocitam, samp1389, sap, sasakrajina, sekretar, simicnenadbg, Sone1983, sova72, suton, svnedelja, tanakadzo, taomaster, tmanda323, Tvrtko I, uruk, USSVoyager, vensla, vidra1, vladao75, vuksa72, x011, Yekaterinburg, Zastava, zastavnik, zdrebac, zmajbre, Zoran1959, zubri, zvomar