read specific file with specific extension

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lordrt
Forum Commoner
Posts: 34
Joined: Sun Jul 19, 2009 11:44 pm

read specific file with specific extension

Post 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:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: read specific file with specific extension

Post 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
(#10850)
lordrt
Forum Commoner
Posts: 34
Joined: Sun Jul 19, 2009 11:44 pm

Re: read specific file with specific extension

Post 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')
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: read specific file with specific extension

Post by VladSun »

As arborint sgguested take a look at http://www.php.net/manual/en/function.glob.php
Example #1
There are 10 types of people in this world, those who understand binary and those who don't
lordrt
Forum Commoner
Posts: 34
Joined: Sun Jul 19, 2009 11:44 pm

Re: read specific file with specific extension

Post 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:
Post Reply