diddy1234 Posted May 13, 2010 Share Posted May 13, 2010 I was wondering if someone could help me. I have a script that gives a listing of a directory and files, however if the file has spaces the file is listed correctly but it does not appear to handle a selected file. so for example if a file is shown as 'test 123.txt' the selected file will be 'test' with the 123.txt chopped off. The same applies to directory names with spaces as well. How do i handle this ? The script is a mixture of what i found on the web and my own customisation to get a directory to be selectable. Thanks in advance. script :--- function ListFiles($path, $tdir) { // List files // $filearray=array(); $dirarray=array(); $dirs = scandir($tdir); $i=0; $l=0; foreach ($dirs as $file) { if($file != '.' && $file != '..') { if (is_dir($tdir . '/' . $file)) { // Is dir $dirarray[$l]=$file; $l++; } else { // Must be a file then, $filearray[$i]=$file; $i++; } } } // Put into order sort($filearray); sort($dirarray); echo "<table border=\"0\" cellspacing=\"0\">"; for($l=0; $l<sizeof($dirarray); $l++) { $dir=$dirarray[$l]; echo "Directory of: <a href=\"?mode=list&dir=" . $tdir . $dir . "\">" . $dir . "</a><br>"; } echo "<br>"; for($i=0; $i<sizeof($filearray); $i++) { $film = substr($filearray[$i], 0 , -4); echo "<tr>"; echo "<td><a href=\"?mode=player&dir=" . $tdir . "&film=" . $film . "\">" . $film . "</a></td>"; } echo "</table>"; } Link to comment https://forums.phpfreaks.com/topic/201676-file-and-directory-listing-not-working-if-files-have-spaces/ Share on other sites More sharing options...
beta0x64 Posted May 17, 2010 Share Posted May 17, 2010 You are selecting the file by creating a link, right? Well, then you have to make your input suitable for a link. Try this function: http://www.php.net/manual/en/function.urlencode.php Link to comment https://forums.phpfreaks.com/topic/201676-file-and-directory-listing-not-working-if-files-have-spaces/#findComment-1059640 Share on other sites More sharing options...
diddy1234 Posted May 27, 2010 Author Share Posted May 27, 2010 Thanks for the reply. could you give a short example of where I would put that function in ? Would I use this 'urlencode' next to the $film and $dir variables ? thanks again for the reply. Link to comment https://forums.phpfreaks.com/topic/201676-file-and-directory-listing-not-working-if-files-have-spaces/#findComment-1063998 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.