Jump to content

Read all files from folder and handle the lines separately


Recommended Posts

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!

 

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.

 

$handle=opendir($path);
while (($file = readdir($handle))!==false) {
if ($file != '.' && $file != '..') {
	$content = file_get_contents("$path/$line");
	echo $content;
}
}
closedir($handle);

 

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.

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>
";
}
?>

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.