How to browse a directory for a file? – PHP

  php

Q(Question):

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?

Thank you
JD.

A(Answer):

Jane Doe wrote:

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?

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.

Regards,
Shawn

Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

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

A(Answer):

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..

A(Answer):

adamt wrote:

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..

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
sh***@glassgiant.com
http://www.glassgiant.com

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

A(Answer):

Shawn Wilson wrote:

Jane Doe wrote:

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?

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.

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
sh***@glassgiant.com
http://www.glassgiant.com

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

A(Answer):

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

Uzytkownik "Shawn Wilson" <sh***@glassgiant.com> napisal w wiadomosci
news:40***************@glassgiant.com…

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.

Regards,
Shawn

Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

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

A(Answer):

On Tue, 13 Jan 2004 20:05:42 -0500, "Chung Leong"
<ch***********@hotmail.com> wrote:

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

Thx everyone 🙂

JD.

LEAVE A COMMENT