tags:

views:

71

answers:

5

I have a number of text files held in directory

/results/...

All the text files are named with unixtime stamps, inside each of the following files there is:

#text¬test¬test1¬test2¬test3¬test4¬1262384177

Each piece of text is seperated by '¬'.

I'd then like to feed the contents of the text file into an array and output it, in for example a table, but for each of the files (Perhaps loop-like?)

If have this but it only works for one file and fixed file name:

$filename = "results/unixtime.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

$array01 = explode("¬",$contents);
$count = count($array01);

echo "<table width = 500 border=1 cellpadding=4>";
$i=0;
for ($i=0;$i<$count;$i++) {
echo "<tr><td>";
echo $array01[$i];
echo "</td></tr>";
}
echo "</table>";
+1  A: 

You can get all the files located in "result" via opendir.

There is also an example ...

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>
Tim
+2  A: 

A couple of things:

  1. Unless you're dealing with really large files just use file_get_contents() to load files. It's a one-liner versus three lines of code that you just don't need;
  2. Loop over arrays using foreach unless you explicitly need a loop counter. The loop condnition/counter is just another area where you can make simple errors;
  3. Use opendir(), readdir() and closedir() for reading directory contents; and
  4. Directories will contain entries like "." and "..". Use filetype() and/or a check on the name and/or extension to limit it to the files you're interested in.

Example:

$directory = "results/";
$dir = opendir($directory);
while (($file = readdir($dir)) !== false) {
  $filename = $directory . $file;
  $type = filetype($filename);
  if ($type == 'file') {
     $contents = file_get_contents($filename);
     $items = explode('¬', $contents);
     echo '<table width="500" border="1" cellpadding="4">';
     foreach ($items as $item) {
       echo "<tr><td>$item</td></tr>\n";
     }
     echo '</table>';
  }
}
closedir($dir);
cletus
I'm getting a syntax error, unexpected '{'
danit
"Unless you're dealing with really large files just use `file_get_contents()`..." - `file_get_contents()` has $offset and $maxlen parameters, also according to the manual "is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.". Any reason not to use it with large files?
Alix Axel
@Alex: TBH I've never actually dealt with files in PHP that have been large enough (over, say, 20MB) that I've been at all concerned about the memory impact of file_get_contents().
cletus
+2  A: 

I suggest the fairly-unknown glob function to detect all your files. Then with all the filenames in a handy array, just iterate through and open up/read each one. Sort of like this:

$files = glob('*.txt');
while(list($i, $filename) = each($files)){
   //what you have now
}
brianreavis
Thanks for sharing that. I will be using that in the future :)
Ally
+1  A: 

Grab the files in the directory and read each filename.

<?php 

if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") {
            $filename = $file;
            //your code
        }
    }
    closedir($handle);
}

?>

source: http://php.net/manual/en/function.readdir.php

Ally
A: 

Here is a more elegant way of writing brianreavis solution, also use file_get_contents instead of fopen, fread and fclose, it's faster and less verbose.

foreach (glob('*.txt') as $filename)
{
    $contents = file_get_contents($filename);
}
Alix Axel