reading/writing on text files

I´d like to know if it´s possible to work with TEXT FILES in PHP scripts. I need to implement a counter which must read a text file (composed by only one line, an integer number), increment this number and then rewrite this text file.

I´ve been using the following script in PERL to make this work for me: (who knows this bizarre language called PERL here? :-))

open(fha,"counter.txt");
$ct=<fha>;
close(fha);

open(fha,">counter.txt");
$ct++;
print fha,$ct;
close(fha);
 
- on the first three lines, the file "counter.txt" is opened for input, and the value of its first line (a number) is stored in the variable $ct

- on the following lines the file is opened for output, $ct is incremented and "counter.txt" is rewritten with the new incremented value (print fha,$ct)

so... Is it possible to substitute this script in PERL by one in PHP? Or isnt possible reading/writing text files in PHP?

thanks a lot,

Correa
CorreaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oubelkasCommented:
Check out the following link. Till now I haven't used .txt files, but if I would I'd use this :

http://www.php.net/manual/html/feature-fileupload.html

See if this can help

Joseph

0
gravityCommented:
Well, you could use a database instead, but the following code'll work with the filename 'counter' in the current directory of the php script.

<?
      $file = fopen('counter','r');
      $currentcount = fread($file,filesize('counter'));
      $newcount = $currentcount+1;
      fclose($file);
      $file = fopen('counter','w-');
      fwrite($file,$newcount);
      fclose($file);
?>

If you have any permission denied problems, remember that the file must be readable by the apache daemon... user nobody works for me.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.