469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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
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.
Jul 17 '05 #1
6 10936
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.
Jul 17 '05 #2
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..

Jul 17 '05 #3
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.
Jul 17 '05 #4
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.
Jul 17 '05 #5
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.

Jul 17 '05 #6
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.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Eric Twietmeyer | last post: by
4 posts views Thread by Larry Brindise | last post: by
5 posts views Thread by Mike Moore | last post: by
3 posts views Thread by Highlander | last post: by
7 posts views Thread by nicknack | last post: by
reply views Thread by William Gill | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.