Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2011, 10:10 PM   PM User | #1
midnite
New Coder

 
Join Date: Jun 2011
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
midnite is an unknown quantity at this point
How do i open a file directory, loop through the files and retrieve the data

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:

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:

Code:
	$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.
midnite is offline   Reply With Quote
Old 11-14-2011, 01:09 AM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,234
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
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().
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 11-14-2011, 05:38 AM   PM User | #3
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
You might find doing all this easier if you use glob() instead.
webdev1958 is offline   Reply With Quote
Old 11-14-2011, 08:16 AM   PM User | #4
midnite
New Coder

 
Join Date: Jun 2011
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
midnite is an unknown quantity at this point
Quote:
Originally Posted by Spookster View Post
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))
midnite is offline   Reply With Quote
Old 11-14-2011, 04:57 PM   PM User | #5
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,234
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
Quote:
Originally Posted by midnite View Post
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.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:04 PM.


Advertisement
Log in to turn off these ads.