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

  • 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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...