I need help with my index.php script. . .

Using applications, configuring, problems
Post Reply
Message
Author
Todd
Posts: 311
Joined: Thu 31 Aug 2006, 18:25

I need help with my index.php script. . .

#1 Post by Todd »

Can anyone help me get this index.php script to list files alphabetically?
<?

$dir_handle = @opendir("./") or die("Unable to open directory!");

while (false !== ($file = readdir($dir_handle))) {
echo "<a href=\"$file\">$file</a><br />";
}

closedir($dir_handle);

?>
Sincerely,

Todd

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#2 Post by MU »

you can use the code from the script I use at dotpups.de.

Instead of printing the filenames directly, they are added to an array.
Then sorted with "asort".

Finally printed.
http://dotpups.de/dotpups/Internet/index.php.txt

For your small example, it might look like this (not tested):

Code: Select all

<? 

$files=array();

$dir_handle = @opendir("./") or die("Unable to open directory!"); 
 
while (false !== ($file = readdir($dir_handle))) { 
  array_push($files,$file);
} 
 
closedir($dir_handle);

 asort($files);

foreach ($files as $file){
  echo "<a href=\"$file\">$file</a><br />"; 
}
?>
Mark

Todd
Posts: 311
Joined: Thu 31 Aug 2006, 18:25

Is there a way. . .

#3 Post by Todd »

Is there a way to hide the index.php file?

Todd

P.S.: I have already tested the modified script and it works great!

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#4 Post by MU »

Code: Select all

<? 
 
 $files=array(); 
 
 $dir_handle = @opendir("./") or die("Unable to open directory!"); 
   
 while (false !== ($file = readdir($dir_handle))) { 
   if ($file != "index.php"){
     array_push($files,$file); 
   }
 } 
   
 closedir($dir_handle); 
 
  asort($files); 
 
 foreach ($files as $file){
     echo "<a href=\"$file\">$file</a><br />"; 
 } 
 ?> 

Todd
Posts: 311
Joined: Thu 31 Aug 2006, 18:25

Thank you!

#5 Post by Todd »

Thank you! It works perfect!

Sincerely,

Todd

raffy
Posts: 4798
Joined: Wed 25 May 2005, 12:20
Location: Manila

webmaster

#6 Post by raffy »

We have a budding webmaster here. :D
Puppy user since Oct 2004. Want FreeOffice? [url=http://puppylinux.info/topic/freeoffice-2012-sfs]Get the sfs (English only)[/url].

Todd
Posts: 311
Joined: Thu 31 Aug 2006, 18:25

I need more help. . .

#7 Post by Todd »

MU, I was wondering if I could pick your brain again. I would like to have the file size listed as well. This is what my file looks like right now:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>http://www.toddrichardson.com/archive/</title>


</head>


<body>

<div style="margin-left: 40px;">

<span style="font-size: 12pt; font-family: Helvetica,Arial,sans-serif; color: rgb(102, 102, 102);">

<br>

<?

$files=array();

$dir_handle = @opendir("./") or die("Unable to open directory...");

while (false !== ($file = readdir($dir_handle))) {
if ($file != "index.php" && $file != "." && $file != "..") {
array_push($files,$file);
}
}

closedir($dir_handle);

asort($files);

foreach ($files as $file) {
echo "<a href=\"$file\">$file</a><br><br />";
}

?>

</span>

</div>

</body>
</html>
I am familiar with html but am just starting w/ php.

Todd

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#8 Post by MU »

yes, that was a bit tricky.

Here is the code.
Note, that I use a HTML-table to "format" the entries.

Mark

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <title>http://www.toddrichardson.com/archive/</title>


</head>


<body>

<div style="margin-left: 40px;">

<span style="font-size: 12pt; font-family: Helvetica,Arial,sans-serif; color: rgb(102, 102, 102);">

<br>

<? 
  
  $files=array(); 
  
  $dir_handle = @opendir("./") or die("Unable to open directory..."); 
  
  while (false !== ($file = readdir($dir_handle))) { 
    if ($file != "index.php" && $file != "." && $file != "..") { 
      array_push($files,$file); 
    } 
  } 
  
  closedir($dir_handle); 
  
  asort($files); 

  print "<table border=0 cellspacing=2 cellpadding=0>";

  foreach ($files as $file) { 

    print "<tr><td><a href=\"$file\">$file</a>";
        $status=stat("$file");
        $filesize = $status[7];
        $timestamp=date("l, d.m.y, H:i",$status[9]);
        $timestamp=preg_replace( "/^[^,]*,/" , "" , $timestamp);
    print "</td><td>&nbsp;&nbsp;<font color=\"#66FF66\">$filesize</font>&nbsp;&nbsp; </td><td> $timestamp</td></tr>\n";
  }
print "</table>";
?> 

</span>

</div>

</body>
</html>

Todd
Posts: 311
Joined: Thu 31 Aug 2006, 18:25

Thank you!

#9 Post by Todd »

Thank you! You know, there are no resources on the web for this type of thing. You are honestly the only source I have. Thank you.

Todd

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#10 Post by MU »

You're welcome :)
There are sources, but usually they are part of big CMS systems, and cannot be used "standalone".

As I needed customized solutions for some projects, I started writing my own small scripts, that I could combine independant from each other.

A good help for me were code-snippets from the function-list of http://php.net/

Mark

raffy
Posts: 4798
Joined: Wed 25 May 2005, 12:20
Location: Manila

examples

#11 Post by raffy »

A good help for me were code-snippets from the function-list of http://php.net/
Yes, Todd, that site abounds with examples of PHP scripts. Just try to think about the job you want and do a function search - it will give you a listing of the nearest useful functions.
Puppy user since Oct 2004. Want FreeOffice? [url=http://puppylinux.info/topic/freeoffice-2012-sfs]Get the sfs (English only)[/url].

Post Reply