Jump to content

[SOLVED] How to implement urlencode in directory listing script


DaveRandom

Recommended Posts

Hi

 

First thing to say is a complete PHP beginner, so please assume I know nothing in any answers (I know a bit but very patchy and widely ranged)

 

I have this script to list all the files of a given directory on a page (mostly borrowed code).

 

<?php

$path = "uploads";

$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Directory Listing of <i><b>$path</b></i><br><br>";

while ($file = readdir($dir_handle)) 
{
   if($file!="." && $file!="..")
      echo "<a href='uploads/$file'>$file</a><br>";
}

closedir($dir_handle);

?>

 

I am trying to work urlencode in so the link path is encoded but the text displayed on the link is not affected. I have been trying various ways to create another variable (I've been calling it $filelink) and manipulate that into an encoded version of $file. However, I can't seem to come up with anything other than infinite circles in the while loop, resulting in nothing else happening on the page after the script.

 

Please help!

Sorted.

 

while ($file = readdir($dir_handle)) 
{
   if($file!="." && $file!="..")
      echo "<a href='uploads/". urlencode ($file)."'>$file</a><br>";
}

 

Also should really be using rawurlencode to be fully standards compliant.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.