Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create a text file of a directory listing?

Posted on 2003-12-08
7
Medium Priority
?
1,263 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

927 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