Show items in directory with links.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
djohnson
Forum Newbie
Posts: 9
Joined: Tue Aug 24, 2004 11:32 am
Location: here

Show items in directory with links.

Post by djohnson »

I have a script that shows items in a directory. Now i want to beable to select them for downloading.

This is my script.

Code: Select all

<?
foreach ( glob ( '/path/*' ) as $item ) echo $item . '<br />';
?>

What can i add to give me links to these files. ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
foreach ( glob ( '/path/*' ) as $item ) 
{
      echo "<a href='/path/".$item."'>".$item."</a> <br />";
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glob returns full path... so you'll need to strip everything leading up to that directory off and add in the web accessible directory structure instead..
User avatar
djohnson
Forum Newbie
Posts: 9
Joined: Tue Aug 24, 2004 11:32 am
Location: here

Ok now what about downloading the files in that dir.

Post by djohnson »

Ok now what about downloading the files in that dir. Do i need to put the server name in there like feyd said? Well i did this

Code: Select all

<?php 
foreach ( glob ( 'http://server/path/*' ) as $item ) 
{ 
      echo "<a href='/path/".$item."'>".$item."</a> <br />"; 
} 
?>
Now it just shows a blank directory. Do i need to specify permissions on the web server? The web sever is IIS 5.0.


?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glob is for local paths only.
User avatar
djohnson
Forum Newbie
Posts: 9
Joined: Tue Aug 24, 2004 11:32 am
Location: here

Post by djohnson »

Sorry im new to this what would i use instead of it?
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

This thread seems relevant for my question. In OS X, I know .app's are just really directories. But is there anyway to have the link download the whole app instead of treating it like a directory and showing the package contents?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

djohnson wrote:Sorry im new to this what would i use instead of it?
ex. 'C:/Program files/Apache/'
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ast3r3x wrote:This thread seems relevant for my question. In OS X, I know .app's are just really directories. But is there anyway to have the link download the whole app instead of treating it like a directory and showing the package contents?
similar, but not..

have the script package the directory/app into a compressed archive for a nice neat download.
User avatar
djohnson
Forum Newbie
Posts: 9
Joined: Tue Aug 24, 2004 11:32 am
Location: here

Post by djohnson »

i think i just lost it.
this is what i have

Code: Select all

<?php 
foreach ( glob ( 'c:/inetpub/phproot/files/*' ) as $item ) 
{ 
      echo "<a href='c:/inetpub/phproot/files/".$item."'>".$item."</a> <br />"; 
} 
?>
it shows the links, but when i selct the file with the line, it shows it trying to access file:///c:/inetpub/phproot/files/c:/inetpub/phprooot/files/fileselected.php
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

feyd wrote:glob returns full path... so you'll need to strip everything leading up to that directory off and add in the web accessible directory structure instead..
User avatar
djohnson
Forum Newbie
Posts: 9
Joined: Tue Aug 24, 2004 11:32 am
Location: here

Post by djohnson »

Hey man thanks for your quick response.

Code: Select all

<?php 
foreach ( glob ( 'http://63.82.108.66/files/*' ) as $item ) 
{ 
      echo "<a href='http://63.82.108.66/files/".$item."'>".$item."</a> <br />"; 
} 
?>
shows no files when i select it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:?

Code: Select all

<?php
$dir = 'c:/inetpub/phproot/files/';
foreach ( glob ( $dir . '*' ) as $item )
{
      echo "<a href='http://63.82.108.66/files/".preg_replace('#^' . preg_quote($dir,'#').'#',$item)."'>".$item."</a> <br />";
}
?>
User avatar
djohnson
Forum Newbie
Posts: 9
Joined: Tue Aug 24, 2004 11:32 am
Location: here

Post by djohnson »

Awsome i can now download files. But i do get one error.

Warning: Wrong parameter count for preg_replace() in C:\Inetpub\phproot\Files\view2.php on line 14
c:/inetpub/phproot/files/view.php

Thanks alot man
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops..

preg_replace('#^' . preg_quote($dir,'#').'#','',$item)
Post Reply