Jump to content

Recommended Posts

how can i make this code not show subdirs?

also (not as important) but i would like to strip teh extension from the filename in the link so when the link is displayed it will look like thisfile instead of thisfile.ext

 

<?php
	$dir = "./downloads/";
if ($handle = opendir('./downloads')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
  {
          	$thelist .= '<li><a href="'.$dir.''.$file.'">'.$file.'</a></li>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>
	</ul>

Link to comment
https://forums.phpfreaks.com/topic/183351-php-directory-list-listing-subdirs-too/
Share on other sites

Maybe try is_file() instead of !is_dir(). I actually had a very similar problem when creating my templating system. I originally used !is_dir() and for some reason it still gave me directories. I tried is_file() and it worked just as I expected it to.

 

Edit: Also, to remove the file-extension, something like this should suffice:

<?php
$pieces = explode('.', $file);
array_pop($pieces);
if(count($pieces) > 1) $file = implode('.', $pieces);
?>

ok i tried the following code and now it is not showing anything at all. did i mis use the is_file()? You may have to spoon feed this to me, i have never used PHP for files before.

 

as for the extension removing it should look like this?

<h1>Other<span class="style1">Files</span></h1>
  <ul>
  <?php
  
  $dir = "./downloads/";
if ($handle = opendir('./downloads')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && is_file($file))
   {
     $pieces = explode('.', $file);array_pop($pieces);
   $file = implode('.', $pieces);
  
           $thelist .= '<li><a href="'.$dir.''.$file.'">'.$pieces.'</a></li>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>
  </ul>

 

<h1>Other<span class="style1">Files</span></h1>
      <ul>
      <?php
      $dir = "./downloads/";
if ($handle = opendir('./downloads')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && is_file($file))
     {
             $thelist .= '<li><a href="'.$dir.''.$file.'">'.$file.'</a></li>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>
      </ul>

Perhaps, you need to evaluate with an actual boolean value:

 

if ($file != "." && $file != ".." && is_file($file)===true)

 

...or...

 

if ($file != "." && $file != ".." && is_dir($file)===false)

 

EDIT: As for removal of the file-extension -- it should be something like this:

 

         if ($file != "." && $file != ".." && is_file($file))
  {
        $pieces = explode('.', $file);
                array_pop($pieces);
	$file_name = implode('.', $pieces);

          	$thelist .= '<li><a href="'.$dir.''.$file.'">'.$file_name.'</a></li>';
          }

 

If you overwrite your variable like that, your Hyperlink will point to "/download/file" instead of "/download/file.ext"

ok here is the code now with the latest changes, 3 things,

 

A. if i use is_dir i still get the subdirs

B. if i use is_file i get nothing

C. if i uncomment the code to strip teh extension i get Array as the link although the link is pointed to the right file.

 

UPDATE: ok with the extension taken off i lose the subdir listings, however i have a break in the list where the subdirs used to be. (the extensions are being removed.)

 

Again thank you so much for all the help. i really appreciate this. Now if we can get rid of the extra line that the subdirs are creating would be great.

 

you can see what i am talking about here http://biogamingservers.com/downloads.php

 

<h1>Other<span class="style1">Files</span></h1>
      <ul>
      <?php
      
      $dir = "./downloads/";
if ($handle = opendir('./downloads')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && is_dir($file)===false)
     {
         $pieces = explode('.', $file);array_pop($pieces);
         $file = implode('.', $pieces);
         $file_name = implode('.', $pieces);
      
             $thelist .= '<li><a href="'.$dir.''.$file.'">'.$file_name.'</a></li>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>
      </ul>

I think I found the problem:

Try this in your IF-condition:

is_dir($dir.$file) === false

 

I believe the problem is that is_dir() works from the directory in which the PHP script is working in. This means if you set your directory to read a subdirectory, the functions inside the loop will not act as if you're looking for files in the parent directory (of the subdirectory you're reading).

 

Edit: Here is the script I used (I copied yours) which works on my server:

<?php
      $thelist = '';
      $dir = './downloads/';
      $handle = opendir($dir);
  while (false !== ($file = readdir($handle)))
      {
          if ($file != '.' && $file != '..' && is_dir($dir.$file) === false){
              $pieces = explode('.', $file);array_pop($pieces);
       	      $file = implode('.', $pieces);
              $file_name = implode('.', $pieces);
          $thelist .= '<p><a href="'.$dir.''.$file.'">'.$file_name.'</a></p>';
        }
      }
  closedir($handle);
echo $thelist;?>

ok well ... that kinda work ... upon trying this code in the following manner it is listing all files in other subdirs as well...

 

my page is proke up in 3 include files left mid and right i broke the downloads into different subdirs (gaming, computer, and misc) i put the code into the right.inc as we discussed and now i am seeing not only the files in the misc subdir of downloads but any file inside any subdir of the main ....

 

also the links to teh files are not using the .extension i even tried to remove the extension strip code and that didnt work either.

 

you can look at the link and see what i mean i know i havent explained very clear.

 

link: http://biogamingservers.com/downloads.php

 

Dir structure

Site_Root

  Downloads

      Computers

      Gaming

      Misc

 

download.php (located in site root)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>.:BIO:. - Downloads</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<style type="text/css">
<!--
.style1 {color: #df6e22}
.style2 {color: #FFFFFF}
-->
</style>
</head>
<body>
<div id="container">
<div id="nav-container"><?php include("includes/nav.inc"); ?></div>
<div id="left">
  <?php include("includes/dlleft.inc"); ?>
  </div>
<div id="middle">
  <img src="images/main_banner.gif" alt="" />
  <?php include("includes/dlmid.inc"); ?>
</div>
<div id="right">
  <?php include("includes/dlright.inc"); ?>
  </div>
<div id="footer">
  RE-Coded by .:BIO:. Archangel</div>
</div>
</body>
</html>

 

dlleft.inc (located in site_root/includes)

<h1>Gaming<span class="style1">Files</span></h1>
  <ul>
   <?php
  
  $dir = "./downloads/gaming/";
if ($handle = opendir('./downloads/gaming')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && is_dir($dir.$file) === false)
   {
     $pieces = explode('.', $file);
   array_pop($pieces);
   $file = implode('.', $pieces);
   $file_name = implode('.', $pieces);
           $thelist .= '<li><a href="'.$dir.''.$file.'">'.$file_name.'</a></li>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>
  </ul>
<h1>Daily<span class="style1">Screenie</span></h1>
<div class="box-bg"><img src="../images/ct_affiliate.jpg" width="88" height="31" /></div>
<h1>Computer<span class="style1">Files</span></h1>
  <ul>
   <?php
  
  $dir = "./downloads/computer/";
if ($handle = opendir('./downloads/computer')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && is_dir($dir.$file) === false)
   {
     $pieces = explode('.', $file);
   array_pop($pieces);
   $file = implode('.', $pieces);
   $file_name = implode('.', $pieces);
           $thelist .= '<li><a href="'.$dir.''.$file.'">'.$file_name.'</a></li>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>
  </ul>

 

dlright.inc (located same as dlleft.inc)

<h1>Ventrilo<span class="style1">Clients<span class="style2">3.0.1</span></span></h1>
  <ul>
   <li><a href="./downloads/vent/ventrilo-3.0.5-Windows-i386.exe" target="_blank">Windows - 32bit</a></li>
   <li><a href="./downloads/vent/ventrilo-3.0.5-Windows-x64.exe" target="_blank">Windows - 64bit</a></li>
   <li><a href="./downloads/vent/ventrilo-3.0.4-Darwin-universal.pkg.zip" target="_blank">Mac OSX 10.3 or higher - 32bit</a></li>
   <li></li>
   <li></li>
   <li></li>
   <li></li>
  </ul>
  <h1>Other<span class="style1">Files</span></h1>
  <ul>
  <?php
  
  $dir = "./downloads/misc/";
if ($handle = opendir('./downloads/misc')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && is_dir($dir.$file) === false)
   {
     //$pieces = explode('.', $file);
   //array_pop($pieces);
   //$file = implode('.', $pieces);
   //$file_name = implode('.', $pieces);
           $thelist .= '<li><a href="'.$dir.''.$file.'">'.$file.'</a></li>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>
  </ul>

Well, regarding the extensionless links: remove this from your scripts (I accidentally left this bit of code in there which I shouldn't have because it overwrites the actual filename) -- so remove this: $file = implode('.', $pieces); and that should work.

 

Regarding your file-listing: Are you getting the results of both lists of filenames in your second include? What I mean by this: example...

/misc/

-- file1

-- file2

-- file3

/gaming/

-- foo

-- bar

-- twix

 

would be the structure; does your list from dlright.php look like this?

-- file1

-- file2

-- file3

-- foo

-- bar

-- twix

instead of just this (which is what you want)?

-- foo

-- bar

-- twix

instead of using readdir to get all the files in a directory, you could use glob. You can just use a wildcard for the name and extension like so

$glob = glob("path/to/*.*");

foreach($glob as $glob){
echo $glob."<br />";
}

http://us2.php.net/glob

 

then you wouldn't have to worry about getting directories. After that you would apply any filters you wanted to. You could also edit the pattern to only get certain types of files, and things like that. Check out the manual for more information

ok dlright.inc should be showing the hard coded links to Vent on the top and on bottom should only be showing the links to any files in the %siteurl%/downloads/misc

 

then the dlleft.inc should be showing

Top section

ONLY any files in the %siteurl%/downloads/gaming dir

bottom section

ONLY any files in the %siteurl%/downloads/computers dir

 

does that clear it up ?

@mikesta707,

i really wouldnt mind using blob, but even after looking at the site i for the life of me cant figure it out, if you could show me a working example of listing the files in a specific dir, and creating a link for each file and then displaying each lin inside of a <li></li> that would help me understand its functions as well as furthering me on my quest ...

As mikesta707 suggested, glob would be much easier to use. I should've suggested that in the first place but it was completely absent from my memory.

 

Any way (according to what I comprehended from your explanation), it sounds like, you have to reset your variable before looping through each of the directories (BEFORE THE WHILE() LOOP): $thelist = '';

something like this should work

<?php
$dir = "your/dir/here";
$glob = glob("path/to/*.*");
$thelist = "";
foreach($glob as $file){
$name = explode('.', $file);
array_pop($name);
$file = implode('.', $name);
$thelist .= '<li><a href="'.$dir.''.$file.'">'.$file.'</a></li>';
}

echo $thelist;
?>

 

just edit as needed

ok that is almost working,

just a few things

 

the link is being generated with the path in front of the file name without the extension, and teh url is being generated without the extension

 

so i am getting ./downloads/misc/Skyplayer and when clicked on it takes me to misc/filename instead of misc/filename.ext

It's because you're overwriting the $file variable. You have to put the extension-less name in its own variable (which I will call $filename):

Replace the current code in your foreach() loop with this:

        $pieces = explode('.', $file);
array_pop($pieces);
$filename = implode('.', $pieces);
         $thelist .= '<li><a href="'.$dir.''.$file.'">'.$filename.'</a></li>';

ok that made things a bit better, however

the link is still being displayed as

./downloads/computer/filename

 

the current code

<?php
$glob = glob("./downloads/misc/*.*");
$thelist = "";
foreach($glob as $file)
{
$pieces = explode('.', $file);	
array_pop($pieces);	
$filename = implode('.', $pieces);         
$thelist .= '<li><a href="'.$file.'">'.$filename.'</a></li>';
}
echo $thelist;
?>

my bad

 

<?php
$dir = "your/dir/here";
//$glob = glob("path/to/*.*");
$glob = glob("*.*");
$thelist = "";
foreach($glob as $file){
$name = explode('.', $file);
array_pop($name);
$name = implode('.', $name);
$thelist .= '<li><a href="'.$dir.''.$file.'">'.$name.'</a></li>';
}

echo $thelist;
?>

 

that should work better

mike you had it right the script isnt running in the folder with the files, so i will have to define the glob as =path/to/*.*

 

the only issue i am having now is the way the link is displayed. see last post for update

ok that is doing great, but it is not pulling the files from the specified folder only the files that are in the folder that the script is running from, how do i tell glob to look in a specific folder and keep it from displaying the link with the dir structue?

ok i went back to the other code and managed to get that to work,

 

Thank you both very much! i really wish there was some sort of karma system to where i could give you guys kudos.

 

Mike i am sorry but i dont know enough about php to use the glob effectively, although the code worked it would only work if i was runing teh script from the directory it was searching for files. below is the code i got to work.

 

 

<?php    
$dir = "./downloads/computer/";
$thelist=""; 
if ($handle = opendir('./downloads/computer')) 
{   
while (false !== ($file = readdir($handle)))
{          
if ($file != "." && $file != ".." && is_dir($dir.$file) === false)   
{     
$pieces = explode('.', $file);   
array_pop($pieces);   
//$file = implode('.', $pieces);   
$file_name = implode('.', $pieces);           
$thelist .= '<li><a href="'.$dir.''.$file.'">'.$file_name.'</a></li>';
          }     
          }  
         closedir($handle); 
          }
          ?>
          <?=$thelist?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.