How to browse a directory for a file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jane Doe

    #1

    How to browse a directory for a file?

    Hi,

    I need to add a download function in a VB program, but since a file
    can be located anywhere in our /download section on the web, I was
    thinking of putting a PHP script there that would take the filename as
    parameter, and browse through the /download directory to look for this
    file, including sub-directories, and return the URL, ie.

    IN


    OUT


    Does anyone have working code to achieve this?

    Thank you
    JD.
  • Shawn Wilson

    #2
    Re: How to browse a directory for a file?

    Jane Doe wrote:[color=blue]
    >
    > Hi,
    >
    > I need to add a download function in a VB program, but since a file
    > can be located anywhere in our /download section on the web, I was
    > thinking of putting a PHP script there that would take the filename as
    > parameter, and browse through the /download directory to look for this
    > file, including sub-directories, and return the URL, ie.
    >
    > IN
    > http://www.acme.com/download/find.php?file=myfile.txt
    >
    > OUT
    > www.acme.com.com/download/sub1/myfile.txt
    >
    > Does anyone have working code to achieve this?[/color]

    You could probably use exec and the linux "find" command to do this.

    Or, you could make a recursive function that reads files of a directory using
    opendir, readdir, closedir and compares the filename to the one requested using
    preg_match. If it encounters a directory (is_dir) you could call the same
    function to traverse it. Caution: be careful not to traverse "..". Also, make
    sure that the user can't steal files from outside the download directory


    or


    This is actually a pretty easy function to write.

    Regards,
    Shawn
    --
    Shawn Wilson
    shawn@glassgian t.com


    I have a spam filter. Please include "PHP" in the
    subject line to ensure I'll get your message.

    Comment

    • adamt

      #3
      Re: How to browse a directory for a file?

      On Tue, 13 Jan 2004 13:14:26 -0400, Shawn Wilson wrote:

      I was doing something similar to this, and ended up smashing my head
      against the wall for hours, and all because I had forgotten to ensure that
      the case of the filename was correct.

      Grrr! Why can't you find 'XmasCard.jpg'? It's right there! Smack!
      Grrrr!
      (repeat ad cranium-fracture)

      Later....

      oh, hang on a minute.... what if I use 'strtolower'?

      Honey, I think I got it working! Are there any head bandages in the
      house?


      Just thought I'd share that, to save you any concussions..

      Comment

      • Shawn Wilson

        #4
        Re: How to browse a directory for a file?

        adamt wrote:[color=blue]
        >
        > On Tue, 13 Jan 2004 13:14:26 -0400, Shawn Wilson wrote:
        >
        > I was doing something similar to this, and ended up smashing my head
        > against the wall for hours, and all because I had forgotten to ensure that
        > the case of the filename was correct.
        >
        > Grrr! Why can't you find 'XmasCard.jpg'? It's right there! Smack!
        > Grrrr!
        > (repeat ad cranium-fracture)
        >
        > Later....
        >
        > oh, hang on a minute.... what if I use 'strtolower'?
        >
        > Honey, I think I got it working! Are there any head bandages in the
        > house?
        >
        > Just thought I'd share that, to save you any concussions..[/color]

        Ah, yes. You can also use something like:

        preg_match("/something/i", $var);

        The "i" after the last delimiter denotes case-insensitive search.

        Regards,
        Shawn

        --
        Shawn Wilson
        shawn@glassgian t.com


        I have a spam filter. Please include "PHP" in the
        subject line to ensure I'll get your message.

        Comment

        • Shawn Wilson

          #5
          Re: How to browse a directory for a file?

          Shawn Wilson wrote:[color=blue]
          >
          > Jane Doe wrote:[color=green]
          > >
          > > Hi,
          > >
          > > I need to add a download function in a VB program, but since a file
          > > can be located anywhere in our /download section on the web, I was
          > > thinking of putting a PHP script there that would take the filename as
          > > parameter, and browse through the /download directory to look for this
          > > file, including sub-directories, and return the URL, ie.
          > >
          > > IN
          > > http://www.acme.com/download/find.php?file=myfile.txt
          > >
          > > OUT
          > > www.acme.com.com/download/sub1/myfile.txt
          > >
          > > Does anyone have working code to achieve this?[/color]
          >
          > You could probably use exec and the linux "find" command to do this.
          >
          > Or, you could make a recursive function that reads files of a directory using
          > opendir, readdir, closedir and compares the filename to the one requested using
          > preg_match. If it encounters a directory (is_dir) you could call the same
          > function to traverse it. Caution: be careful not to traverse "..". Also, make
          > sure that the user can't steal files from outside the download directory
          >
          > http://www.acme.com/download/find.php?file=/etc/passwd
          > or
          > http://www.acme.com/download/find.ph...ris_hilton.mov
          >
          > This is actually a pretty easy function to write.[/color]

          Oh, I forgot to mention that this function could return more than one result or
          it could just return the first instance of the filename. You have to consider
          how you want it to handle that. For that matter, the linux 'find' command could
          return more than one result as well.

          Regards,
          Shawn
          --
          Shawn Wilson
          shawn@glassgian t.com


          I have a spam filter. Please include "PHP" in the
          subject line to ensure I'll get your message.

          Comment

          • Chung Leong

            #6
            Re: How to browse a directory for a file?

            Also, watch out the symbolic links so the function won't don't end up
            calling itself ad infinitum.

            Uzytkownik "Shawn Wilson" <shawn@glassgia nt.com> napisal w wiadomosci
            news:40042772.A 3EA0439@glassgi ant.com...[color=blue]
            > Or, you could make a recursive function that reads files of a directory[/color]
            using[color=blue]
            > opendir, readdir, closedir and compares the filename to the one requested[/color]
            using[color=blue]
            > preg_match. If it encounters a directory (is_dir) you could call the same
            > function to traverse it. Caution: be careful not to traverse "..". Also,[/color]
            make[color=blue]
            > sure that the user can't steal files from outside the download directory
            >
            > http://www.acme.com/download/find.php?file=/etc/passwd
            > or
            > http://www.acme.com/download/find.ph...ris_hilton.mov
            >
            > This is actually a pretty easy function to write.
            >
            > Regards,
            > Shawn
            > --
            > Shawn Wilson
            > shawn@glassgian t.com
            > http://www.glassgiant.com
            >
            > I have a spam filter. Please include "PHP" in the
            > subject line to ensure I'll get your message.[/color]


            Comment

            • Jane Doe

              #7
              Re: How to browse a directory for a file?

              On Tue, 13 Jan 2004 20:05:42 -0500, "Chung Leong"
              <chernyshevsky@ hotmail.com> wrote:[color=blue]
              >Also, watch out the symbolic links so the function won't don't end up
              >calling itself ad infinitum.[/color]

              Thx everyone :-)

              JD.

              Comment

              Working...