Directory listing with file info

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kim Jensen

    Directory listing with file info

    I'd like to make a directory listing where instead of the entire
    filename I need it to show the filename minus the extention and get the
    value of charname= in the file itself.

    I've been told that I had to turn the directory listing into an array
    and then use "foreach (array as item)" to go through and open each file
    but I've tried several different approaches and I just can't get it to work.

    I've been able to make it list the directory in order using this script
    but after that I'm lost.

    <?
    $list = Array();
    $handle = opendir('testdi r/.');
    while (false !== ($file = readdir($handle ))) {
    if ($file != "." && $file != "..") {
    $list[] = ($file);
    }
    }
    closedir($handl e);
    sort ($list);
    reset ($list);

    while (list ($key, $val) = each ($list)) {
    echo "<a href=test.php?n ame=$val>$val</a><br>";

    }
    ?>

    Here I'd like the $val after the name= to be just the filename without
    the extention (all files in the directory are txt files) and then the
    second $val which it shows in the list to be the value of the line
    charname= in the txt files themselves.

    Anybody have an idea what I should do?

    Kim Jensen

  • Erich Musick

    #2
    Re: Directory listing with file info

    Try this:

    <?
    $list = Array();
    $handle = opendir('../');
    while (false !== ($file = readdir($handle ))) {

    $fileparts = explode(".",$fi le);
    if (count($filepar ts) > 1) {
    array_pop($file parts);
    }
    $file = join(".",$filep arts);

    if ($file != "." && $file != ".." && $file != "") {
    $list[] = $file;
    }
    }
    closedir($handl e);
    sort ($list);
    reset ($list);

    while (list ($key, $val) = each ($list)) {
    echo "<a href=test.php?n ame=$val>$val</a><br />";

    }

    ?>

    Erich Musick

    Kim Jensen wrote:[color=blue]
    > I'd like to make a directory listing where instead of the entire
    > filename I need it to show the filename minus the extention and get the
    > value of charname= in the file itself.
    >
    > I've been told that I had to turn the directory listing into an array
    > and then use "foreach (array as item)" to go through and open each file
    > but I've tried several different approaches and I just can't get it to
    > work.
    >
    > I've been able to make it list the directory in order using this script
    > but after that I'm lost.
    >
    > <?
    > $list = Array();
    > $handle = opendir('testdi r/.');
    > while (false !== ($file = readdir($handle ))) {
    > if ($file != "." && $file != "..") {
    > $list[] = ($file);
    > }
    > }
    > closedir($handl e);
    > sort ($list);
    > reset ($list);
    >
    > while (list ($key, $val) = each ($list)) {
    > echo "<a href=test.php?n ame=$val>$val</a><br>";
    >
    > }
    > ?>
    >
    > Here I'd like the $val after the name= to be just the filename without
    > the extention (all files in the directory are txt files) and then the
    > second $val which it shows in the list to be the value of the line
    > charname= in the txt files themselves.
    >
    > Anybody have an idea what I should do?
    >
    > Kim Jensen
    >[/color]

    Comment

    • Pedro Graca

      #3
      Re: Directory listing with file info

      Kim Jensen wrote:[color=blue]
      > while (list ($key, $val) = each ($list)) {
      > echo "<a href=test.php?n ame=$val>$val</a><br>";
      >
      > }
      >
      > Here I'd like the $val after the name= to be just the filename without
      > the extention (all files in the directory are txt files)[/color]

      echo '<a href="test.php? name=', substr($val, -4), ">$val</a><br";
      [color=blue]
      > and then the
      > second $val which it shows in the list to be the value of the line
      > charname= in the txt files themselves.[/color]

      Are you really sure you want to do that?
      You have top open each and every file, read/print its contents, and close
      the file. It might be a lot slow!


      /* needs error-checking for fopen() and fgets() */
      foreach ($list as $val) {
      $f = fopen($val);
      while (!feof(f)) {
      $line = fgets($f);
      if (preg_match('/^charname=(.*)$/', $line, $matches)) {
      echo '<a href="test.php? name=', urlencode(subst r($val, -4)),
      '">', $matches[1], '</a><br>';
      break; /* leave inner while() */
      }
      }
      fclose($f);
      }
      --
      Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
      == ** ## !! !! ## ** ==
      TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
      bypass the spam filter. I will answer all pertinent mails from a valid address.

      Comment

      • Erich Musick

        #4
        Re: Directory listing with file info

        This method, though, assumes that all extensions are exactly three
        characters long.

        What about extensions such as pl, py, h, among others?



        Pedro Graca wrote:[color=blue]
        > Kim Jensen wrote:
        >[color=green]
        >>while (list ($key, $val) = each ($list)) {
        >> echo "<a href=test.php?n ame=$val>$val</a><br>";
        >>
        >>}
        >>
        >>Here I'd like the $val after the name= to be just the filename without
        >>the extention (all files in the directory are txt files)[/color]
        >
        >
        > echo '<a href="test.php? name=', substr($val, -4), ">$val</a><br";
        >
        >[color=green]
        >>and then the
        >>second $val which it shows in the list to be the value of the line
        >>charname= in the txt files themselves.[/color]
        >
        >
        > Are you really sure you want to do that?
        > You have top open each and every file, read/print its contents, and close
        > the file. It might be a lot slow!
        >
        >
        > /* needs error-checking for fopen() and fgets() */
        > foreach ($list as $val) {
        > $f = fopen($val);
        > while (!feof(f)) {
        > $line = fgets($f);
        > if (preg_match('/^charname=(.*)$/', $line, $matches)) {
        > echo '<a href="test.php? name=', urlencode(subst r($val, -4)),
        > '">', $matches[1], '</a><br>';
        > break; /* leave inner while() */
        > }
        > }
        > fclose($f);
        > }[/color]

        --
        In Christ,

        Erich Musick


        In the same way, let your light shine before others, so that they may
        see your good works and give glory to your Father who is in heaven. --
        Matthew 5:16 NKJV

        Comment

        • Kim Jensen

          #5
          Re: Directory listing with file info

          Pedro Graca wrote:[color=blue]
          > Are you really sure you want to do that?
          > You have top open each and every file, read/print its contents, and close
          > the file. It might be a lot slow![/color]

          The problem is that the filenames themselves even if they contain the
          name I'm looking for aren't really pleasing to the eye as links.

          I'd rather have links that read:

          Angelina Jolie
          Gwynneth Paltrow
          Jude Law
          Michael Gambon
          Sir Lawrence Olivier

          than

          angelinajolie
          gwynnethpaltrow
          judelaw
          lawrenceolivier
          michaelgambon

          Sorry, just got home from watching Sky Captain :)

          Kim Jensen

          Comment

          • Tenzel Kim

            #6
            Re: Directory listing with file info

            Kim Jensen wrote:[color=blue]
            > Pedro Graca wrote:
            >[color=green]
            >> Are you really sure you want to do that?
            >> You have top open each and every file, read/print its contents, and close
            >> the file. It might be a lot slow![/color]
            >
            >
            > The problem is that the filenames themselves even if they contain the
            > name I'm looking for aren't really pleasing to the eye as links.
            >
            > I'd rather have links that read:
            >
            > Angelina Jolie
            > Gwynneth Paltrow
            > Jude Law
            > Michael Gambon
            > Sir Lawrence Olivier
            >
            > than
            >
            > angelinajolie
            > gwynnethpaltrow
            > judelaw
            > lawrenceolivier
            > michaelgambon
            >
            > Sorry, just got home from watching Sky Captain :)[/color]

            Just forgot something. If I was just now creating the whole thing I
            could easily use the first examples as file names, but unfortunately I
            already have thousands of links using the second type of file names so
            going back and changing all of them manually or even by global
            search/replace would take ages.

            Kim Jensen

            Comment

            • Pedro Graca

              #7
              Re: Directory listing with file info

              Erich Musick top-posted (and I changed the format):[color=blue]
              > Pedro Graca wrote:[color=green]
              >> Kim Jensen wrote:
              >>[color=darkred]
              >>>Here I'd like the $val after the name= to be just the filename without
              >>>the extention (all files in the directory are txt files)[/color][/color][/color]

              ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^
              [color=blue][color=green]
              >> echo '<a href="test.php? name=', substr($val, -4), ">$val</a><br";[/color]
              >
              > This method, though, assumes that all extensions are exactly three
              > characters long.[/color]

              Yes, as the OP stated.
              [color=blue]
              > What about extensions such as pl, py, h, among others?[/color]

              In this case, it didn't seem necessary to make a generic function -- but
              I agree that would have been a better option from the start :)

              --
              Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
              == ** ## !! !! ## ** ==
              TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
              bypass the spam filter. I will answer all pertinent mails from a valid address.

              Comment

              • Kim Jensen

                #8
                Re: Directory listing with file info

                Pedro Graca wrote:[color=blue]
                > Are you really sure you want to do that?
                > You have top open each and every file, read/print its contents, and close
                > the file. It might be a lot slow![/color]

                About the slowness. Couldn't it be done so that it only took the files
                starting with a specific letter?

                So that if I want to see a listing of the files starting with A I'd open
                dir.php?letter= a

                Kim Jensen

                Comment

                • Erich Musick

                  #9
                  Re: Directory listing with file info



                  Pedro Graca wrote:[color=blue]
                  > Erich Musick top-posted (and I changed the format):
                  >[color=green]
                  >>Pedro Graca wrote:
                  >>[color=darkred]
                  >>>Kim Jensen wrote:
                  >>>
                  >>>
                  >>>>Here I'd like the $val after the name= to be just the filename without
                  >>>>the extention (all files in the directory are txt files)[/color][/color]
                  >
                  >
                  > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^
                  >
                  >[color=green][color=darkred]
                  >>>echo '<a href="test.php? name=', substr($val, -4), ">$val</a><br";[/color]
                  >>
                  >>This method, though, assumes that all extensions are exactly three
                  >>characters long.[/color]
                  >
                  >
                  > Yes, as the OP stated.
                  >
                  >[/color]

                  My bad - failed to notice that :)
                  [color=blue][color=green]
                  >>What about extensions such as pl, py, h, among others?[/color]
                  >
                  >
                  > In this case, it didn't seem necessary to make a generic function -- but
                  > I agree that would have been a better option from the start :)
                  >[/color]

                  --
                  In Christ,

                  Erich Musick


                  In the same way, let your light shine before others, so that they may
                  see your good works and give glory to your Father who is in heaven. --
                  Matthew 5:16 NKJV

                  Comment

                  • Erich Musick

                    #10
                    Re: Directory listing with file info

                    Yeah...that's a possibility


                    Kim Jensen wrote:[color=blue]
                    > Pedro Graca wrote:
                    >[color=green]
                    >> Are you really sure you want to do that?
                    >> You have top open each and every file, read/print its contents, and close
                    >> the file. It might be a lot slow![/color]
                    >
                    >
                    > About the slowness. Couldn't it be done so that it only took the files
                    > starting with a specific letter?
                    >
                    > So that if I want to see a listing of the files starting with A I'd open
                    > dir.php?letter= a
                    >
                    > Kim Jensen
                    >[/color]

                    --
                    In Christ,

                    Erich Musick


                    In the same way, let your light shine before others, so that they may
                    see your good works and give glory to your Father who is in heaven. --
                    Matthew 5:16 NKJV

                    Comment

                    • Pedro Graca

                      #11
                      Re: Directory listing with file info

                      [ (-: Tenzel Kim Jensen? ]

                      Tenzel Kim wrote:[color=blue]
                      > Kim Jensen wrote:[color=green]
                      >> I'd rather have links that read:
                      >>
                      >> Angelina Jolie
                      >>
                      >> than
                      >>
                      >> angelinajolie[/color]
                      >
                      > If I was just now creating the whole thing I
                      > could easily use the first examples as file names, but unfortunately I
                      > already have thousands of links using the second type of file names so
                      > going back and changing all of them manually or even by global
                      > search/replace would take ages.[/color]

                      Write a script to do that for you!

                      <?php
                      $oldfiles_dir = 'old/';
                      $newfiles_dir = 'new/';

                      foreach ($list as $val) {
                      /* get $charname :: fopen(), fgets(), fclose() */
                      copy($oldfiles_ dir . $val, $newfiles_dir . $charname);
                      }

                      ?>

                      And presto! :-)
                      --
                      Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
                      == ** ## !! !! ## ** ==
                      TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
                      bypass the spam filter. I will answer all pertinent mails from a valid address.

                      Comment

                      • Pedro Graca

                        #12
                        Re: Directory listing with file info

                        Erich Musick wrote:[color=blue]
                        > Pedro Graca wrote:[color=green]
                        >> Erich Musick top-posted (and I changed the format):[color=darkred]
                        >>>What about extensions such as pl, py, h, among others?[/color]
                        >>
                        >> In this case, it didn't seem necessary to make a generic function -- but
                        >> I agree that would have been a better option from the start :)[/color]
                        >[/color]

                        What about
                        filename.with.a .lot.of.dots
                        and
                        filename_withou t_dots
                        ?

                        :-)


                        The generic function would have to deal with them!

                        --
                        Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
                        == ** ## !! !! ## ** ==
                        TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
                        bypass the spam filter. I will answer all pertinent mails from a valid address.

                        Comment

                        • Pedro Graca

                          #13
                          Re: Directory listing with file info

                          Kim Jensen wrote:[color=blue]
                          > Pedro Graca wrote:[color=green]
                          >> Are you really sure you want to do that?
                          >> You have top open each and every file, read/print its contents, and close
                          >> the file. It might be a lot slow![/color]
                          >
                          > About the slowness. Couldn't it be done so that it only took the files
                          > starting with a specific letter?
                          >
                          > So that if I want to see a listing of the files starting with A I'd open
                          > dir.php?letter= a[/color]

                          Or you could make a 'index' file and only open that one. Its contents
                          would be something like:

                          angelinajolie.t xt Angelina Jolie
                          gwynnethpaltrow .txt Gwynneth Paltrow
                          judelaw.txt Jude Law
                          lawrenceolivier .txt Sir Lawrence Olivier
                          michaelgambon.t xt Michael Gambon


                          Or use a database.


                          Of course these options assume the 'index' file and the database would
                          be updated for every new file ... (and deletions and changes).

                          --
                          Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
                          == ** ## !! !! ## ** ==
                          TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
                          bypass the spam filter. I will answer all pertinent mails from a valid address.

                          Comment

                          • Erich Musick

                            #14
                            Re: Directory listing with file info

                            The code i posted earlier should work with that...

                            Pedro Graca wrote:[color=blue]
                            > Erich Musick wrote:
                            >[color=green]
                            >>Pedro Graca wrote:
                            >>[color=darkred]
                            >>>Erich Musick top-posted (and I changed the format):
                            >>>
                            >>>>What about extensions such as pl, py, h, among others?
                            >>>
                            >>>In this case, it didn't seem necessary to make a generic function -- but
                            >>>I agree that would have been a better option from the start :)[/color]
                            >>[/color]
                            >
                            > What about
                            > filename.with.a .lot.of.dots
                            > and
                            > filename_withou t_dots
                            > ?
                            >
                            > :-)
                            >
                            >
                            > The generic function would have to deal with them!
                            >[/color]

                            --
                            In Christ,

                            Erich Musick


                            In the same way, let your light shine before others, so that they may
                            see your good works and give glory to your Father who is in heaven. --
                            Matthew 5:16 NKJV

                            Comment

                            • Kim Jensen

                              #15
                              Re: Directory listing with file info

                              Pedro Graca wrote:[color=blue]
                              > [ (-: Tenzel Kim Jensen? ][/color]

                              Heh. That's the problem with having a split personality. Sometimes you
                              forget which is which :)

                              Kim Jensen or was that Tenzel Kim???

                              Comment

                              Working...