Web Hosting Talk







View Full Version : urlencode?


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.";
?>

brendandonhu
11-04-2006, 04:29 PM
Here's a start. It skips hidden all hidden files like .ftpquota, .htaccess, etc., but that can be changed if it isn't what you wanted. It does show directories, not sure if that's what you wanted or not.

<?
$loc = 'folder';
$site = 'http://www.domain.com/' . $loc;
$dir = '/home/folder/www/' . $loc;
foreach(glob($dir . '/*') as $file)
{
printf('<a href="%s">%s</a><br>', $site . '/' . basename($file), basename($file));
}
echo '<br /><br />Use your browser\'s back button to return to this page.';
?>

brendandonhu
11-04-2006, 04:35 PM
Just saw your edit...I believe spaces were truncating filenames because you didn't quote the attribute values for <a>. You would have been looking for rawurlencode() rather than urlencode(), although I think my above code will handle filenames with spaces without doing either.

bear
11-04-2006, 04:39 PM
Cheers, Brendan. That works fine and considerably more elegant than mine....off to read up on glob now.
Just checked, and the unquoted href attribute was indeed causing it. Thanks twice, then. ;)

brendandonhu
11-04-2006, 04:41 PM
No problem :)

horizon
11-05-2006, 10:59 AM
I'd like to send my inputs on this subject.

I'd recommend changing:


$dir = '/home/folder/www/' . $loc;


for:


$dir = (@is_dir('/home/folder/www/' . $loc)) ? @dirname('/home/folder/www/' . $loc) : "";


Then, add below:


if ($dir) {


Then, add below:


printf('<a href="%s">%s</a><br>', $site . '/' . basename($file), basename($file));
}


this:


}


;)

ThatScriptGuy
11-05-2006, 01:31 PM
Why in the world would you want to do that? $dir is static - non-changing. Why take the resources to see if it's a directory at all?

That's just a plain waste of resources there. We understand that you can use ternary operators..and we're all very proud. But your code is no better than the code before it...

horizon
11-05-2006, 02:12 PM
Why take the resources to see if it's a directory at all?


In case the directory would not exist.


But your code is no better than the code before it...

No need to get defensive there ... I didn't post my codes so it could be better ... it was just posted as a suggestion. If you do not want to use it, it's alright. :)

ThatScriptGuy
11-05-2006, 02:13 PM
But it's wrong and completely unnecessary. There is no need for your little corrections that essentially accomplish nothing but a waste of resources..

horizon
11-05-2006, 02:14 PM
Very well. Sorry for helping you on this.

brendandonhu
11-06-2006, 01:47 PM
Have you read the manual entry for glob? If the directory doesn't exist, my code returns an empty array and the loop isn't executed. Your code would give exactly the same output as mine except it's longer.

xgoth3
11-06-2006, 08:12 PM
I learnt something new today: glob() function.
Thank you brendan.

I wrote a similar script for the same purpose, with only 5 lines of code with readdir();

Now i can use yours with only 3 lines of code: :agree:

foreach(glob($dir . '/*') as $file){
printf('<a href="%s">%s</a><br>', $site . '/' . basename($file), basename($file));
}

brendandonhu
11-06-2006, 08:51 PM
You're welcome :)