bear
11-04-2006, 04:05 PM
urlencode($file);
After using readdir to grab file names from a directory, I'm trying to pass the result through something to take any spaces in the filename and convert it into something useful since I'm wrapping a URL around it. If I just read, it's fine. When I use the same result inside a URL, any spaces then truncate the filename, breaking the link. If I encode using this, it converts the space to a +. This is what PHP apparently wants it to do, but I need the space perhaps converted to % 20? (space added).
Being a PHP newb, this is something I just can't seem to fix, nor locate through searching. Can anyone point me in the right direction here?
Here's the full code so far.
<?
$loc = "folder";
$site = "http://www.domain.com/$loc/";
$dir = "/home/folder/www/$loc/";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file !=".ftpquota" && $file != ".htaccess") {
$filename = urlencode($file);
echo "<a href=$site$filename>$filename</a><br>";
}
}
closedir($handle);
}
echo "<br /><br />Use your browser's back button to return to this page.";
?>
After using readdir to grab file names from a directory, I'm trying to pass the result through something to take any spaces in the filename and convert it into something useful since I'm wrapping a URL around it. If I just read, it's fine. When I use the same result inside a URL, any spaces then truncate the filename, breaking the link. If I encode using this, it converts the space to a +. This is what PHP apparently wants it to do, but I need the space perhaps converted to % 20? (space added).
Being a PHP newb, this is something I just can't seem to fix, nor locate through searching. Can anyone point me in the right direction here?
Here's the full code so far.
<?
$loc = "folder";
$site = "http://www.domain.com/$loc/";
$dir = "/home/folder/www/$loc/";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file !=".ftpquota" && $file != ".htaccess") {
$filename = urlencode($file);
echo "<a href=$site$filename>$filename</a><br>";
}
}
closedir($handle);
}
echo "<br /><br />Use your browser's back button to return to this page.";
?>
