...

View Full Version : How do i open a file directory, loop through the files and retrieve the data



midnite
11-13-2011, 10:10 PM
Hi there guys;

I was wondering if anyone could give me a little help i have a script that opens a file and returns the total files requested, the total files requests in the articles directory, the total bandwidth used, the total 404 Errors and the files the produced those errors, the problem is it only opens 1 file and i need it to open 2 or 3 or even more files and return the same data but with the code that i have if i want to open 2 or 3 files i will have to duplicate the code which is not the best aproach i also know that one possible solution would be to store the files in a directory and use opendir instead of fopen, i've tried to implement the opendir but i just cant get it to work, here is the current code:


<?php

// Opens the folder in the reading mode
$fileLog = fopen("may.log", "r");

// Variable to count the total bytes used during the month
$totalBytes = 0;

// Variable to count the total files requested during the month
$linecount = 0;

$file_requested = 0;

// Variable to count the total errors found
$file_not_found = 0;

// Variable assigned to array that stores and displays the description of the errors found
$errors = array();
$articles_file = 0;

// While not the end of file get and echo the data line by line
while (!feof($fileLog)) {
$line = fgets($fileLog, 1024);

$linecount = $linecount + substr_count($line, "\n");

// Explodes the data with a space
$details = explode(' ', $line);


// Checks if the variable is set if yes returns true if no returns an Notice on the site
if (isset($details[8]))

// Adds all the bytes and stores them in $totalBytes
$totalBytes = $totalBytes +$details[8];

if (isset($details[7]))
if ($details[7] == "404") {
$file_not_found++;

if(!in_array($details[5], $errors)) {
$errors[] = $details[5];
}

}

if (isset($details[5]))
$directory_string = $details[5];

$directory_array = explode("/",$directory_string);

if($directory_array[0] == "articles") {
$file_requested++;

}
}
fclose($fileLog);


// Assigns the variable $totalBytes to $total and rounds it up to 7 decimal places
$total = round(($totalBytes/1000000),2);

// Adds commas every 3 digits
$totalBytes = number_format($totalBytes);


echo "<h3>May Statistics</h3>";

// Echoes the variable $linecount
echo "<p>The Total files requested: $linecount </p>";
echo "<p>Total files requests the articles directory: $file_requested </p>";

// echoes the total bytes
echo "<p>The Total bandwidth used: $total MB ($totalBytes Bytes)</p>";

// Echoes the the variable $file_not_found
echo "<p>Total 404 Errors: $file_not_found </p>";
echo "<p>The following files produced errors:</p>";

// Assigns the variable $errors to $error and echoes the result in a unordered list
echo "<ul>";

foreach ($errors as $error) {

echo "<li>$error</li>";
}
echo "</ul>";

?>

Now i've been working on the following script:


$fileStored = opendir('./hoe7b_data');
while(false !== ($file = readdir($fileStored))) {
if($file !="." && $file !="..") {
echo"$file<br />";
}
}
closedir($fileStored);

which allows me to see the files inside the directory but how can i retrieve the data like i did on the first script?? how can i apply the code from the first script to this one??

Can anyone help please im just a little lost...Any ideas or examples would be more then welcome.

Thank you so much.

Spookster
11-14-2011, 01:09 AM
Well in your second script you've gotten the file handle using readdir() ($file) so you would use that in your first script with fgets($file). This would replace getting the file handle from fopen().

webdev1958
11-14-2011, 05:38 AM
You might find doing all this easier if you use glob() (http://php.net/manual/en/function.glob.php) instead.

midnite
11-14-2011, 08:16 AM
Well in your second script you've gotten the file handle using readdir() ($file) so you would use that in your first script with fgets($file). This would replace getting the file handle from fopen().

So your saying i should replace the fopen() in the first script with fgets()? and while (!feof($fileLog)) while(false !== ($file = readdir($fileLog))

Spookster
11-14-2011, 04:57 PM
So your saying i should replace the fopen() in the first script with fgets()? and while (!feof($fileLog)) while(false !== ($file = readdir($fileLog))


That's not what I said.

To use file reading functions you need a file handle which is a reference to the file.

fopen() returns a file handle
readdir() returns a file handle

fgets() reads a file using a file handle.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum