Jump to content

Need PHP help


Recommended Posts

Does anyone know how I can write a script that:

 

- reads in all the files in a particular directory

- displays the file names in a html list and makes a link of them:

<ul>

<li><a href="filelocation1">filename 1</li>

<li><a href="filelocation2">filename 1</li>

<li><a href="filelocation3">filename 1</li>

</ul>

 

etc. ?

 

So basically it creates a list of links with the contents in that directory,

so you can download them from there.

Link to comment
Share on other sites

<?php
$dir	= '/tmp';
$files1 = scandir($dir);
?>

 

 

then just loop through the array and print them.

 

for ($i = 0; $i <= count($files1); $i++)
{
  //print your files here with html markup so you can link
  echo "<a href=\"".$dir."\/".$files1[$i]."\">".$dir."\/".$files1[$i]."</a>";
}

 

top of my head, you might want to check the escaping chars and so on in the echo function cus i dont have a good editor in this little box that corrects me ;) You also have to put the other html tags on there :)

Link to comment
Share on other sites

Yeah, actually usually I use ASP, but PHP runs on it too and I'm trying to learn PHP so it's good practice to use that.

 

MS-only with PHP? :huh:

 

IIS should have something similar like apache I think... but don't know

Link to comment
Share on other sites

  • 2 weeks later...

it'd be better practice to run php on apache with mysql ;)

 

but seriously...wingcom's suggestion earlier should work. i do something similar, though it's more specific to reading jpg files and displaying them:

$files = array();  
$dir = opendir('./Pictures/');  
while(($file = readdir($dir)) !== false)  
{  
if (!is_dir("$directorypath/$file") && (strstr("$file",".jpg") || strstr("$file",".JPG")))  
{  
	$files[] = $file;  
}  
}
closedir($dir); 

for($i=0; $i<count($files); $i++)  
{  
echo "<a href=\"$address/browse.php/$i\" class=\"thumbnail\">";
echo "<img src=\"$address/Pictures/thumbs/$files[$i]\">";
echo "</a>";
}

I do it this way, with the while loop, so that I can check and make sure the file meets certain specs (it's a jpeg and it's not a directory). This probably isn't the best example ever, as I hacked it together late one night.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...