khosh Posted September 28, 2009 Share Posted September 28, 2009 Hi, I'm quite unfamiliar with php. I'm trying to read all files in a directory, open them and handle the lines in the files separately. The files to be read might look like this: 1 Product code: 2 FP50 3 4 Description: 5 Pine 50x100mm 6 7 Price: 8 3.5 I've gotten as far as learning that $file = file(filename); echo $file[0]; displays the first line of a file, and $file[1] the second and so on... What I want to do is add several info files in one folder. I then want to read them all and display them in the same way with one short piece of code that runs through everything and displays the info where I want it, for example: <table> <tr> <td><?php echo $file[1]; ?></td> <td><?php echo $file[4]; ?></td> <td><?php echo $file[7]; ?></td> </tr> </table> This way I only have to add a new file to the folder and the script displays the information in the table. I know it's possible, but I just can't get my head around it. Any help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/ Share on other sites More sharing options...
Zane Posted September 28, 2009 Share Posted September 28, 2009 look at using foreach http://php.net/foreach foreach($file as $line) echo $line . " \n"; Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/#findComment-926238 Share on other sites More sharing options...
khosh Posted September 28, 2009 Author Share Posted September 28, 2009 Thanks for the tip. I thought it might have to do with that. Still, I can't get it to work. I found this code to read through the folder and collect all the filenames: $handle=opendir($path); while (($file = readdir($handle))!==false) { if ($file != '.' && $file != '..') echo $file; } closedir($handle); It outputs a list of the files in the folder. I tried to replace the "echo $file;" with this code: foreach($file as $line) $content = fopen("$path/$line", "r"); echo $content; but now I get "Warning: Invalid argument supplied for foreach() in ... on line..." The foreach doesn't seem to work and I'm not sure I used the fopen command correctly either. Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/#findComment-926252 Share on other sites More sharing options...
Mark Baker Posted September 28, 2009 Share Posted September 28, 2009 $handle=opendir($path); while (($file = readdir($handle))!==false) { if ($file != '.' && $file != '..') { $content = file_get_contents("$path/$line"); echo $content; } } closedir($handle); Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/#findComment-926261 Share on other sites More sharing options...
khosh Posted September 28, 2009 Author Share Posted September 28, 2009 Ok, now I got it to read all the info from the files when I changed the $content = file_get_contents("$path/$line"); to $content = file_get_contents("$path/$file"); Now it displays all info from the files. How do I get it to read specific line numbers from each file and not the whole file? When simply using "echo $content[0]" it only shows the first letter/sign from each file. Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/#findComment-926290 Share on other sites More sharing options...
khosh Posted September 28, 2009 Author Share Posted September 28, 2009 Got it! I just used explode with \n as separator. Huge victory for a newbie! Thanks for all the help, I'll ask more if I encounter other problems... after trying to solve them myself of course! Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/#findComment-926297 Share on other sites More sharing options...
thebadbad Posted September 28, 2009 Share Posted September 28, 2009 glob() is simpler: <?php $path = 'sampledir'; $files = glob("$path/*"); foreach ($files as $file) { $lines = file($file); echo "<table> \t<tr> \t\t<td>{$lines[1]}</td> \t\t<td>{$lines[4]}</td> \t\t<td>{$lines[7]}</td> \t</tr> </table> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/#findComment-926298 Share on other sites More sharing options...
khosh Posted September 28, 2009 Author Share Posted September 28, 2009 So it seems. Short and handy. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/175768-read-all-files-from-folder-and-handle-the-lines-separately/#findComment-926302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.