Why does readdir() read the filename in string till space???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Monopoly
Forum Commoner
Posts: 41
Joined: Wed May 21, 2008 1:19 pm

Why does readdir() read the filename in string till space???

Post by Monopoly »

I have the following code:

$dir="video";
$dh = opendir($dir);
while($file = readdir($dh)){

if ($file!="." && $file!=".."){
echo "<li><a href=list_video.php?dir=$file>$file</a></li><br>";
}

}

It works very well , but if the filename contains spaces within it (example: "tra la la") then the function reads the only a part of the filename , untill it encounters the first space (example: $file = "tra" if it were to be "tra la la".

Are there any solutions to read the whole filename even if it has spaces within it?

Thank You
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Why does readdir() read the filename in string till space???

Post by EverLearning »

readdir() is reading filenames just fine. If you look at the source of your page you will see that the filenames are complete. One problem is that you dont have quotes around the href atrribute value, and second you need to urlencode your filenames.
Try this

Code: Select all

echo "<li><a href=\"list_video.php?dir=" .  urlencode($file) . "\">{$file}</a></li><br>";
Monopoly
Forum Commoner
Posts: 41
Joined: Wed May 21, 2008 1:19 pm

Re: Why does readdir() read the filename in string till space???

Post by Monopoly »

Thanks ! +1
Post Reply