Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    You might find doing all this easier if you use glob() instead.

  • #4
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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))

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •