Solved

Create a text file of a directory listing?

Posted on 2003-12-08
7
1,210 Views
Last Modified: 2006-11-17
This has probably been covered, but an hour of searching hasn't helped me yet...

What I'd like to do is have a php script that would read the contents of a directory, and then write the result to "list.txt" in the same directory. However, I have two requirements of the text file:

(1) it must list only .jpg, .gif. or .png files.
(2) it must generate the list alphabtically. (I thought this would have been automatic, but oddly enough it doesn't seem to that way on my server.)

For example, when I call "the_script_i_require.php" it will hunt throgh this:

directory
|_some_directory_here
|                            |_apple.jpg
|_awesome.jpg
|_bathtub.gif
|_index.html
|_index.php
|_racer.png
|_the_script_i_require.php
|_toyboat.txt

...and leave "list.txt" containing only:

awesome.jpg
bathtub.gif
racer.png

Thanks in advance for any assistance.
0
Comment
Question by:TunaMaxx
  • 2
  • 2
  • 2
  • +1
7 Comments
 

Expert Comment

by:EmielElgersma
ID: 9902963
this will make the list
<?
$handle=opendir($thedir);
clearstatcache();
while ($filename = readdir($handle))
{
      if ($file != "." && $file != "..") {
      
            $ext=substr($filename,-4);
            

            if (($ext == ".gif" OR $ext == ".png" OR $ext == ".jpg") AND file_exists($link))
                                   echo $filename . '\n';
                         
      }

  }
?>
0
 
LVL 5

Expert Comment

by:Insolence
ID: 9903195
Eh, I'm too lazy to write the line or two that needs to be added to that script, but the author did ask for it to be written to list.txt in that directory.  =)

 - Insolence
0
 
LVL 1

Accepted Solution

by:
show_source earned 200 total points
ID: 9903266
Try this.

<?php

$dir_to_read = dirname($_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF']) ."/"; // read the dir where this file is, fullpath is used, instead of ./ in case of safe_mode or open_basedir restrictions

$filetypes = array("jpg", "gif", "jpeg", "png"); // extension of files

$path = opendir($dir_to_read);

$file_list = array();
while (false !== ($files = readdir($path))) {

if($files == "." || $files == ".." || is_dir($dir_to_read . $files)) {
continue;
}

      $ext = pathinfo($files); // used to check file-extension

      if(in_array(@strtolower($ext["extension"]), $filetypes)) { // used @ to avoid error-message, if file has no extension
      $file_list[] = $files;
      }

}
closedir($path);

if(count($file_list) > 0) {
sort($file_list);

$filenames = "";

foreach($file_list as $x) {
$filenames .= $x ."\n";
      }
$file_to_write = $dir_to_read ."list.txt";

$fp = fopen($file_to_write, "w+"); // w+ == create file if it doesn't exists. You may have to use chmod, if you are on a unix system
//chmod($file_to_write, 0777);
if ($fp) {
fputs($fp, $filenames);
fclose($fp);
      }

// Just display filenames to be written
echo $filenames;

}else{
echo"No files found";
}
?>
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:TunaMaxx
ID: 9903375
EmielElgersma: I don't know what I was doing wrong, but I couldn't get your script to work. The best I could get was a half a second or so of page load, and then a blank page. Also, as  Insolence pointed out, there was no text file written anywhere.

show_source: Awesome! Works like a charm, and you even commented everything so I could follow along. The fact that you made the list of names also print out on screen was a bonus I hadn't even considered. However, now that it's there, is there a way to have each filename on a separate line? Right now the display is just a continuous list. I took a wild stab and added a beak to what I *thought* was the right place, but it doesn't seem to work:

I changed:
  // Just display filenames to be written
  echo $filenames;

To:
  // Just display filenames to be written
  echo $filenames . '<br>';

I'll award you the points even if you can't show me how to accomplish that, but I am interested in finding out why I couldn't make it work.

Thanks!
0
 
LVL 1

Expert Comment

by:show_source
ID: 9903391
well, you just put a linebreak after $filenames, but to list the names, put the <br> in the foreach

foreach($file_list as $x) {
$filenames .= $x ."\n";
echo $x ."<br>\n";
     }
0
 

Author Comment

by:TunaMaxx
ID: 9903420
Thank you very much. Great job!
0
 

Expert Comment

by:EmielElgersma
ID: 9903995
Sorry I didn't work for you, I didn't have much time to test it.. but I toke it from an  old script of mine which made a gallery of all images that where in a spec. directory.

Good you got an alternative....
Emiel
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now