Jump to content

[SOLVED] Printing the contents of each file in a directory


ghr

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.