Jump to content

Posting directory contents


SingingCrane

Recommended Posts

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

There...now that [i]that[/i] 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...

[code]
<?
$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;
?>
[/code]

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!! :)
Link to comment
https://forums.phpfreaks.com/topic/21629-posting-directory-contents/
Share on other sites

What about this?

[code]<?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";
    }
  }
}

?>[/code]

Orio.
No need to use explode. Try this...

[code=php:0]
<?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;
?>
[/code]
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!
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...
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 [url=http://keithneilson.co.uk/kgpalpha1.php]http://keithneilson.co.uk/kgpalpha1.php[/url] for the results I only want the JPGs listed, and here's the code:
[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>";
}
}*/
}
}
?>
[/code]

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.