Solved

Create a text file of a directory listing?

Posted on 2003-12-08
7
1,231 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP curl issue VERBOSE output 18 80
Currency in SQL? 2 29
Wordpress Cron - Send XML Data to external server FTP 3 21
simple html dom php accessing table/cell  values 4 18
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

765 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