ghr Posted August 26, 2007 Share Posted August 26, 2007 I've been trying to create a PHP page that reads each file in a directory (txt, for example), and then outputs the contents of the file. The purpose for this is for someone advertising products for a website, so that he can use a form to describe the product, and then saves each entry as a new txt file. I then want the php to print the contents of the file. I've set it up so that it is html code, so formatting is not an issue. I've figured out how to read the contents of a single file, using file_get_contents. I can also display each file NAME from the directory. However, when I add file_get_contents into the loop, the php script wont run. Here's what I have so far: <?php $handle = opendir('sales'); if($handle) { while(false !== ($file = readdir($handle))) { print "$file <br>"; $contents = file_get_contents($file); print "$contents <br>"; } closedir($handle); } ?> I've tried using seperate php entries and also had a play with readf and such, but can't seem to get anything to work. It seems like a common thing to want to do, and I've probably just made a small mistake somewhere? If anyone could give me a hand it would be really appreciated:) Thanks, Gareth Quote Link to comment https://forums.phpfreaks.com/topic/66790-solved-printing-the-contents-of-each-file-in-a-directory/ Share on other sites More sharing options...
wildteen88 Posted August 26, 2007 Share Posted August 26, 2007 Do you want PHP to read and display all files contents within a directory or just specific files? I am not sure what you are trying to do. Prehaps you'll want to use a database instead. Quote Link to comment https://forums.phpfreaks.com/topic/66790-solved-printing-the-contents-of-each-file-in-a-directory/#findComment-334673 Share on other sites More sharing options...
ghr Posted August 26, 2007 Author Share Posted August 26, 2007 Here's an example: index/sales CONTAINS: car1.txt car2.txt ... ________ car1.txt CONTAINS: Make:Ford Model:Fiesta Price: £1000 _________________ car2.txt CONTAINS: Make:Ferrari Model:F40 Price: £87000 _________________ Now I need to make a php program that reads the "index/sales" directory and prints the CONTENTS of each FILE. The output of the above example would be: Make:Ford Model:Fiesta Price: £1000 Make:Ferrari Model:F40 Price: £87000 Yes, a "database" would be the "correct" way to do this, but there's only ever going to be 30 entries at most really, so theres not much point in getting into mySQL. I want this to be as SIMPLE as possible:) Like I said, I've figured out how to print a list of the file names in a certain directory with opendir() and i've figured out how to print the contents of a single file file_get_contents() Now I need to combine the two methods. Hope that helps, Gareth Quote Link to comment https://forums.phpfreaks.com/topic/66790-solved-printing-the-contents-of-each-file-in-a-directory/#findComment-334818 Share on other sites More sharing options...
Fadion Posted August 27, 2007 Share Posted August 27, 2007 Guess the problem in your script is the parameter u are giving to file_get_contents, as actually its reading a file in the directory of your script and not in the folder. Try this: <?php $dir = 'files'; $dirHandle = opendir($dir); while($files = readdir($dirHandle)){ if($files == '.' or $files == '..'){ continue; } $contents = file_get_contents($dir . "/" . $files); echo $files . "<br />"; echo $contents . "<br /><br />"; } closedir($dirHandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66790-solved-printing-the-contents-of-each-file-in-a-directory/#findComment-334824 Share on other sites More sharing options...
ghr Posted August 27, 2007 Author Share Posted August 27, 2007 Hey thanks that was great! I knew it would only be something fairly simple like that, just havn't had enough experience with PHP to figure it out! Here's the final code listing, modified to only read the contents of html files: <?php $dir = 'car_sales'; $dirHandle = opendir($dir); while($files = readdir($dirHandle)) { if($files == '.' or $files == '..' or $files == 'jpg') { continue; } if(preg_match("/\w+(.html)/",$files)){ $contents = file_get_contents($dir . "/" . $files); echo $files . "<br />"; echo $contents . "<br /><br />"; } } closedir($dirHandle); ?> Thanks Guys! Gareth Quote Link to comment https://forums.phpfreaks.com/topic/66790-solved-printing-the-contents-of-each-file-in-a-directory/#findComment-335391 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.