PHP read data from several text files, outputting a certain line from all files?

$dir = opendir('directory'); while($file = readdir($dir)){ if ($file! = ". " or $file!

= ".."){ $opened = file($file); echo $opened4. ""; } }.

Say if the number of products continues to grow. I.e. We may have 100+ by Xmas :) – JaneKealum Aug 24 at 18:07 Uh you're missing loads of steps... fopen just returns a resource, no t the file content ;) – Rudu Aug 24 at 18:07 @Rudu: changed alreadyyyyyyyy – genesis Aug 24 at 18:07 @JaneKealum: edited for you – genesis Aug 24 at 18:09 @genesis this looks perfect.

Excuse my stupidity, but could you include where I would call in the reference to the directory - I am an old school macro excel programmer :) – JaneKealum Aug 24 at 18:11.

So this is essentially three parts: (1) you need to loop through the directory, (2) reading in the fifth line of each file and (3) you need to read the section of that line, after the colon. // open the products directory $fileHandle = opendir('products'); // create an array to store the file data $files = array(); // create an array to store products $products = array(); if ($fileHandle) { // loop through each file while ( false! == ($singleFile = readdir($fileHandle)) ) { // use file() to read the lines of the file $lines = file($singleFile); // store the fifth line in each file (the array starts at 0) $productDescription = explode(":",$lines4); $products = productDescription1; // the bit after the colon } } // this should show you an array of the products print_r($products).

I'm receiving a server 500 error now. I tried: opendir('products') and opendir('products/'). – JaneKealum Aug 24 at 18:41 You can always run this code from within the products directory and use opendir(".

") – Matthew Aug 25 at 16:19.

PHP to load multiple files: An example file (one. Txt): line 1 line 2 line 3 line 4 Product Desc: iPhone line 6 Shortcuts: Because we only want the fifth line we use the $j loop to read the first five lines each overwriting the last. If the file is less than five lines you'll get a null out of $line... you should test for that (and not add it to the product list).

Finally because we now the length of the string "Product Desc:" (the way you wrote it) we can just throw that first part of the line away. This isn't terrible robust, better to use a RegEx, or string parsing to make sure the right words are there, and then consume the data following the colon. Still... it answers your original question ;) You said there could be many more lines, this approach only loads the first 5 lines into memory (each over writing the last) and stops once the fifth line is reached... which means serious performance and memory advantages over reading the whole file into an array (file) and then only using line 5.

Design Decisions: It looks like you're building a product catalog, it would be much better to use a database to store this data. Doesn't have to be a big deal (MySQL, PostgreSQL) it could be something as easy as SQLite.

E.g. By Xmas we may have 100+ products! Thanks again – JaneKealum Aug 24 at 18:33 hmm.. having issues now getting the array ti display anything.

Simpy getting a blank screen now. No inputs in the file are being seen? They were before your change however :S – JaneKealum Aug 24 at 18:54 I think im close, however im not having any values outputted?

I am getting the list with the array seeing that there are 25 files. However, no string output for the value of the 5th line for each file. Could this be associated with the global var of 'products'?

I didn't know this was the case... – JaneKealum Aug 24 at 19:19 @JaneKealum I guess I'd add some debugging: first line of getProduct add echo "get $file"; (which will show files are being read - should output 25 names). After the for in that same function add echo "$line"; to see the line that's being captured.. if it's off (not the fifth?) adjust $j accordingly. – Rudu Aug 24 at 19:58.

Answer: pastebin.com/WBBQW9wA (raw code at bottom) The process is three-fold. Get all the filenames in the directory. Read in the fifth line each file.

Split after the colon. My method is very well documented and understandable. It uses functions to do specific parts of the process.It's quite easy to understand what's going on, and is fairly robust at the same time.

Functions that do the work: Example: $description) { $descriptions$fileName = readAfterColon($description); } //display it echo "\n"; print_r($descriptions); echo "";?

This is exactly what I was looking for in terms of explanation. This is much appreciated, Ive just spent 30 mins analyzing the code carefully tio get a better understanding of the syntax... Having problems however; script is correctly outputting filenames (all 25), however there is no value being displayed for the 5th line in each file? :( – JaneKealum Aug 24 at 19:38 Hmm.. .

Try printing out before the foreach loop and see if you have the raw data with the colon. Perhaps I have a typo in my final code somewhere. – Levi Morrison Aug 24 at 19:54 @JaneKealum I found the error, I had forgotten to include the directory path for the filename.It works beautifully in all my tests, now.

– Levi Morrison Aug 24 at 22:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions