3 Replies - 14457 Views - Last Post: 03 January 2009 - 02:58 PM Rate Topic: -----

#1 Techno Guy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 27-May 08

Exclude some files (.txt .php .html) with scandir

Post icon  Posted 02 January 2009 - 10:55 PM

I used to use curPageURL to get my directory, but the dir was un-ordered, so now I use scandir, which works fine, but I have no idea how to get it to exclude some file types (.txt .php .html), some help would be great :^:

My current code at the moment: (it shows all the files and folders)
<?php


$files = scandir( "./" );

$URLstart = 'http';
$URLstart .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
 $URLstart .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
 $URLstart .= $_SERVER["REQUEST_URI"];
}
if ($_SERVER["HTTPS"] == "on") {$URLstart .= "s";}


for( $ctr = 0; $ctr < sizeof( $files ); $ctr++ ) {
echo "<a href='$URLstart$files[$ctr]'>$URLstart$files[$ctr]</a><br>";
}



?>


Is This A Good Question/Topic? 0
  • +

Replies To: Exclude some files (.txt .php .html) with scandir

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3873
  • View blog
  • Posts: 11,407
  • Joined: 18-April 07

Re: Exclude some files (.txt .php .html) with scandir

Posted 02 January 2009 - 11:27 PM

scandir is going to give you a result set of the files, so what you will need to do is then loop through this set and if it is in an exclude list, skip the file. The easiest way to do this is using an array of excluded file extensions etc and then check each filename against this array.

Something along the lines of...


$files = scandir($root_dir);
$extensions = array(".txt",".php",".htm");

// Loop through each filename of scandir
foreach ($files as $filename) {
	 // Construct a full path
	 $filepath = $root_dir.'/'.$filename;

	 // Is it a file? If so, get the extension using some function you created
	 if(is_file($filepath)) {
		  $ext = getFileExtension($filename);
		  
		  // Is the file extension not included in the array of forbidden extensions?
		  // Since it is not included, execute code to list the file or whatever
		  if (!in_array($ext,$extensions)) {
			   // Code for files not excluded
		  }
	 }
}



That will give you the basic idea of how you can work it.

Hope that helps you out with your project. Enjoy!

"At DIC we be file excluding code ninjas... we hate some files so much we excluded them from our reindeer games" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 Techno Guy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 27-May 08

Re: Exclude some files (.txt .php .html) with scandir

Posted 03 January 2009 - 01:00 AM

Thank you for your help, but can you please help me out a bit more, im still learning php.

Found this[1] code via google, which gets the file extension, but I haven't used functions much so I have no idea how I am supposed to use it...
I added this above your code [2] but it doesn't work :(

[1]
function getFileExtension($filename)
  {
	  $path_info = pathinfo($filename);
	  return $path_info['extension'];
  }



[2]
$root_dir = "./";
$files = scandir($root_dir);
$extensions = array(".txt",".php",".htm");

// Loop through each filename of scandir
foreach ($files as $filename) {
	 // Construct a full path
	 $filepath = $root_dir.'/'.$filename;

	 // Is it a file? If so, get the extension using some function you created
	 if(is_file($filepath)) {
		  $ext = getFileExtension($filename);
		  
		  // Is the file extension not included in the array of forbidden extensions?
		  // Since it is not included, execute code to list the file or whatever
		  if (!in_array($ext,$extensions)) {
			   // Code for files not excluded
			echo "<a href='$URLstart$filename'>$filename</a><br>";

		  }
	 }
}

Was This Post Helpful? 0
  • +
  • -

#4 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 132
  • View blog
  • Posts: 674
  • Joined: 03-August 06

Re: Exclude some files (.txt .php .html) with scandir

Posted 03 January 2009 - 02:58 PM

This is code I took from an old version of my Mimesis package. I'll show you how to make it work for your example:
<?php
/*
Copyright © 2008 Grim Pirate <grimpirate_jrs@yahoo.com>

All rights reserved.

Redistribution and use in source and binary forms, without modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- The name of the Grim Pirate may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * dirContents source file
 *
 * This file contains the code for the dirContents function
 * @author Grim Pirate <grimpirate_jrs@yahoo.com>
 * @link [url="http://mimesis.110mb.com/"]http://mimesis.110mb.com/[/url]
 * @version 1.2
 * @since 1.0e
 * @package Mimesis
 */

error_reporting(E_ALL);

/**
 * A function that returns the contents of a directory recursively
 *
 * @param string $searchDir the uri of the directory to be searched
 * @param array $pregarr an array of file/directory names to be excluded composed of regular expression patterns
 * @param boolean $inclusive causes exclusions to be inclusions
 * @return array an array of file and directory names/paths or FALSE on failure
 */
function dirContents($searchDir, $pregarr = array(), $inclusive = false){
 	$lar = array(array(), array());
	if(false === $handle = opendir($searchDir)) return false;
	while(false !== $link = readdir($handle)){
		if($link !== '.' && $link !== '..'){
			$validLink = true;
			foreach($pregarr as $value){
				$validLink ^= preg_match($value, $link);
			}
			$validLink ^= $inclusive;
			if($validLink){
				$temp = $searchDir . DIRECTORY_SEPARATOR . $link;
				if(is_dir($temp)){
					array_push($lar[0], $temp);
					$temp = dirContents($temp, $pregarr, $inclusive);
					$lar[0] = array_merge($lar[0], $temp[0]);
					$lar[1] = array_merge($lar[1], $temp[1]);
				}else{
					array_push($lar[1], $temp);
				}
			}
		}
	}
	closedir($handle);
	return $lar;
}
?>
Ignore all the copyright stuff and error_reporting and the like. Basically it's just the dirContents you want. So your function call for dirContents would be as follows:
$directoryContents = dirContents($searchDir, array('/^.*\.txt$/i', '/^.*\.php$/i', '/^.*\.html$/i'));
This will return a two-dimensional array. The first nested array will be all the folders in the searched directory and the second nested array will be all the files in the searched directory. Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1