Hi There -

Am trying to list all of the items in a directory, with each item on a new line.

The backslash r backslash n in double quotes is giving me no new line.

One of the things that may be a problem is a double necessity for double quotes. I've tried several ways to fix this. Quotes have always been dastardly for me.

Any help will be gratefully accepted.

Thanks,

Lee

Code below.

<?php
// name this file index.php and include it inside the directory you want the list from

// open the current directory
$dhandle = opendir('.');
// define an array to hold the files
$files = array();

if ($dhandle) {
   // loop through all of the files
   while (false !== ($fname = readdir($dhandle))) {
      // if the file is not this file, and does not start with a '.' or '..',
      // then store it for later display
      if (($fname != '.') && ($fname != '..') &&
          ($fname != basename($_SERVER['PHP_SELF']))) {
          // store the filename
          $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
      }
   }
   // close the directory
   closedir($dhandle);
}

$newline="\r\n";

// Now loop through the files, with a new line after each one

foreach( $files as $fname )
{
   echo "$fname $newline";
}

?>
    Capoeirista wrote:

    The backslash r backslash n in double quotes is giving me no new line.

    How do you know?

    If you're viewing this page in a web browser, try viewing the source of the page. You'll probably see the new lines there.

      Oh Doh!

      I need to insert break tags! This is what I get when I don't sleep.

      I'm trying to get this into a form when I can bring the list into excel, in one column. If there's a better way to think of it, I'm happy to listen. I figured if I can get them to display as a list, one on each line, then I can just copy and paste.

      Any ideas? I'd like to learn how to think better about these things.

      Thanks for your help,

      Lee

        Well, you could ouput the data as if it was a .csv file like so:

        header ('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header ('Content-Type: application/octet-stream');
        header ('Content-Disposition: attachment; filename="files.csv"');
        
        echo implode("\n", $files);

        Then you could simply click "Open" and Excel would open the .csv "file" with the file names in the first column. Note that this assumes Excel is set to handle .csv files on your computer. I also had trouble getting IE to do work properly, so you also have to be using a decent browser (e.g. Firefox).

          Brad, Thanks!

          This is just the cool kind of thinking I want to develop. I have a tendency to come up with the most complex possible way to do something. Your way skips a lot of steps. What do the headers do? I haven't seen them.

          Thanks again!

            The first header just attempts to bypass the browser's cache, in case it tries to use old data (I'm assuming this is dynamically generated content, meaning caching would be bad).

            The second header simply tells the browser what type of content it is.

            The Disposition header is the key - it tells the browser that the user should be promped to open or save the content, rather than just loading it inline (which is an alternative value for 'attachment', IIRC).

              Write a Reply...