Jump to content


Photo

Posting directory contents


  • Please log in to reply
7 replies to this topic

#1 SingingCrane

SingingCrane
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 22 September 2006 - 06:25 AM

Hi my name is S.C. and I am a n00b.

There...now that that is out of the way, I have a question (or two) about posting directory contents.  ;D

I have my W.A.M.P. set up for which I am using to test my code before I post live content onto my site (which is Linux-based). So I have my page all set up to read the directory contents of a specified directory and post them on the page...I should add, specifically as images.

My first question is how do I go about telling the parser to ignore sub-directories?

My second question is how do I specify ONLY the types of files I want it to display, or is the way I am doing it correct? i.e. can I add more distinctions like ($file != ".txt") etc...?

I suppose I should go ahead and post the code I have so far so that you can see where I am at...

<?
$pics = "<div id=\"info\"><p class=\"lbold\">This is a test page for images.</p>\n";
$folder = "PICS/CraterLake";
$dir = opendir($folder);
while($file = readdir($dir)) {
if (($file != "..") and ($file != ".")) {
$pics .= "<a href='$folder/Images/$file'><img src='$folder/$file' /></a>\n";
}
}
$pics .= "</div>";
echo $pics;
?>

Relatively simple...like I said..I'm a n00b. hehe.
As you can see I am calling out the files as link images. The problem is that the parser is including the sub-directories in the same path and showing 'blank' links in addition to the images, even though the "." and ".." files are to be ignored.

Any help is greatly appreciated!! :)

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 22 September 2006 - 07:31 AM

What about this?

<?php

$allowed_types=array("png","jpg","gif") //put here the accepted extensions

while($file = readdir($dir))
{
  if (is_file($file))
  {
    $explode=explode(".",$file);
    if(in_array($explode[count($explode)-1],$allowed_types))
    {
      $pics .= "<a href='$folder/Images/$file'><img src='$folder/$file' /></a>\n";
    }
  }
}

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 SingingCrane

SingingCrane
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 22 September 2006 - 08:20 AM

Hmm...that didn't work for me. Rather, none of the files now appear from the directory. I am a littl confused as to how explode() works, but I do understand the reason for the array.

Not sure where to go with this.

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 22 September 2006 - 08:28 AM

No need to use explode. Try this...

<?php
$arr = array('gif','jpg','png');
$pics = "<div id=\"info\"><p class=\"lbold\">This is a test page for images.</p>\n";
$folder = "PICS/CraterLake";
$dir = opendir($folder);
while($file = readdir($dir)) {
    if (($file != "..") && ($file != ".") && (in_array(substr($file,-3,3),$arr)) {
        $pics .= "<a href='$folder/Images/$file'><img src='$folder/$file' /></a>\n";
    }
}
$pics .= "</div>";
echo $pics;
?>


#5 SingingCrane

SingingCrane
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 22 September 2006 - 08:42 AM

Hey that worked great! Thanks thorpe! I really appreciate it!!

I will point out however that your code needs one more end parenthesis on the if statement (I got an error doing a copy and paste DOH!). Now I just need to break it down in my head as to how your addition works and I am all set! Thanks again!

#6 SingingCrane

SingingCrane
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 22 September 2006 - 08:57 AM

Ok...I think I understand what is going on with this....lemme see if I can 'splain and tell me if I am on the right track or not...

The code is saying 'match' in the array of what the variable $arr specifies [(in_array(substr(start from the 3rd character at the end of * and give a length of 3 characters), variable] and print only that type.

Yay? Nay? I'm really fresh with this stuff and still getting my head around a lot of basic principles...

#7 SingingCrane

SingingCrane
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 22 September 2006 - 09:02 AM

Hmm...another question since we are here (relative to the array)...

Can one specify directories in an array?

#8 mandrill

mandrill
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 September 2006 - 02:19 PM

I'm having the same kind of problem. I'm trying to get a list of files in a subdirectory but it keeps giving me a list of the files in the root directory as well.
see http://keithneilson....k/kgpalpha1.php for the results I only want the JPGs listed, and here's the code:
<?php

/* Keiths Gallery Project
Purpose: To dynamically list files and directories and display them as galleries on my website
Step one: define variables */

$rootdir="/this/is/where/i/put/my/gallery/";
// Step two: generate array of subdirectories of the /gallery directory. These will be my galleries organised according to subject matter
$dh=opendir($rootdir); //open the starting directory
while ($filename = readdir($dh))
	{
	$rootdircont[] = $filename; //put everything in the starting directory in an array
	}
$counter = 0 ;
foreach ($rootdircont as $file)
{
	if ( is_dir($rootdir.'/'.$file)) //check each value in the array to see if it is a directory
	{
		if(($file!='..')or($file!='.')) //trying to filter out the . and .. so they are not opened but it doesn't work
		{	
			$counter ++ ;
			$galarray[$counter] = $file; //if it is a directory put it in another array
		}
	}
}
/* test echo of above */
echo '</br>';
foreach ($galarray as $galdir)
{
	echo "Echo 1: $galdir</br>";
}
/* end test echo */
foreach ($galarray as $galdir) 
{
	$galdiruri="/gallery/$galdir"; // set the value of the gallery's uri
	if(($galdir!='..')or($galdir!='.')) // same as before but doesn't seem to be working, again
	{
		$dh=opendir($rootdir.'/'.$galdir); //open subdirectory
		while ($filename=readdir($dh))
		{
		if(is_file($rootdir.'/'.$galdir.'/'.$filename)) //makes sure you only get filenames in the following array
			{
				$images[] = $filename; // transfer filenames into another array
			}	
		}
		/*test echo list of files*/
		foreach ($images as $test)
		{
			echo "Echo 2: $test</br>";
		}	
		/*foreach ($images as $imagefile) // the following prints the array as a list of links 
		{
			if (is_file($rootdir.'/'.$galdir.'/'.$imagefile)) //make sure any directories aren't listed
			{
			echo"<a href=&quot;";
			echo"$galdiruri";
			echo"/";
			echo"$imagefile";
			echo"&quot;>";
			echo"$imagefile";
			echo"</a></br>";
			}
		}*/
	}
}	
?> 

I've tried changing all sorts of things and it doesn't seem to make much difference (though using is_file gave me better results than I had  got before, I only thought of it when I read this thread.) I'm ignoring the invalid argument for now as I think I know what's causing it.

Singingcrane: If you put everything in the directory you're reading into an array then use if(is_file()) and elseif(is_dir()) that should let you specify things to do with files and things to do with directories. I could be wrong though I'm just as much of a n00b as you.
---------------------------------------------------
I can't affor to pwn,
do you think I could r3nt?
http://keithneilson.co.uk
---------------------------------------------------




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users