Page 1 of 1

read specific file with specific extension

Posted: Wed Jul 29, 2009 1:01 am
by lordrt
Hi again 8)

I am using php script to read files containing lines of text. In the actual case, I will have files names as file1.txt, file2.txt, file3.txt and so on, probably with file4.doc etc. All the files are stored in same folder as phpscript one.

I want this to happen when I execute the script:

1. Script search for first file with .txt extension
The script will have to retrieve the filename as well, I know if only 1 file was read directly it would be fopen('file1.txt'), but in this case the script will read the files dynamically, thus each time it will execute a new file will be read, so will be like each time it gets a new file it will concatenate the name + ext and then use fopen('fileX.txt'). (using cron here to execute script)

2. Read content line-by-line for that file using fgets
3. [some processing will be done] //no code needed for this part
4. Delete the file read once EoF reached
5. When cron is read again, the script will be assumed to search for next .txt file, e.g. file2.txt, and will repeat steps 1-4 again.

Anyone can help me with the code for steps 1,2,4? :banghead:

Re: read specific file with specific extension

Posted: Wed Jul 29, 2009 1:51 am
by Christopher
lordrt wrote:Anyone can help me with the code for steps 1,2,4? :banghead:
PHP has many filesystem functions. See glob(), readfile() and unlink(). I would recommend browsing this whole section (and the manual in general)

http://www.php.net/manual/en/book.filesystem.php

Re: read specific file with specific extension

Posted: Wed Jul 29, 2009 6:57 am
by lordrt
thx arborint but still sounds very complicated to me.

I summarise my prob again:

I have 5 text files in a folder containing script as well. Script has to search for a file ending with .txt, without knowing the file's name. Once it encounters one text file, it "opens" it, read the content, fo the ops and as soon as it reaches EoF it has to delete the file, after executing last op.

The script does not have to list all files present, it reads one file at a time, and stops. On the next call, it again searches for a text file and goes on. The call is done by cron, which can execute every hour, thus it is assumed will take 5 hours to completely read and execute all the files.

the scrip should dynamically call the file, not hardcoded like when using fopen('filename.txt')

Re: read specific file with specific extension

Posted: Wed Jul 29, 2009 7:11 am
by VladSun
As arborint sgguested take a look at http://www.php.net/manual/en/function.glob.php
Example #1

Re: read specific file with specific extension

Posted: Thu Jul 30, 2009 2:48 am
by lordrt
VladSun wrote:As arborint sgguested take a look at http://www.php.net/manual/en/function.glob.php
Example #1
Thanks VladSun, managed to understand the concept :drunk: