ArshSingh Posted March 1, 2014 Share Posted March 1, 2014 (edited) Im Using the followign code to search files from directory but i have to type the extension of the file as well liek ; yo.jpg ,,, if i type only 'yo' search query comes blank ,,, how do i glob search so will get result without typing extension of file : <?phpinclude("db-settings.php"); include("config.php"); $actfolder = $_REQUEST['folder']; $query = $_REQUEST['query']; $directory = "uploads/$loggedInUser->username/"; if (is_dir($directory)) { if ($directory_handle = opendir($directory)) { while (($file = readdir($directory_handle)) !== false) { if ($file == "$query") { $filet = "uploads/$loggedInUser->username$actfolder/$file"; $path_info = pathinfo($filet); if (array_key_exists('extension', $path_info)) { $extension = $path_info['extension']; } else { $extension = "folder"; } switch ($extension) { case "jpg": case "png": case "gif": case "bmp": $filetype = "image"; $actionfile = "<img src=\"$filet\" height=\"130\" width=\"130\">"; $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; break; case "txt": case "doc": case "docx": case "odt": case "ods": case "odp": case "xls": case "xlsx": case "pdf": $filetype = "text"; $actionfile = "<img src=\"img/text.jpg\" height=\"130\" width=\"130\">"; $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; break; case "mp3": $filetype = "sound"; $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; $actionfile = "<img src=\"img/sound.jpg\" height=\"130\" width=\"130\"> "; break; case "ogg": case "wav": $filetype = "sound"; $actionfile = "<img src=\"img/music.jpg\" height=\"130\" width=\"130\">"; $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; break; case "avi": case "mpeg": case "wmv": case "mp4": case "3gp": case "flv": $filetype = "video"; $actionfile = "<img src=\"img/video.jpg\" height=\"130\" width=\"130\">"; $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; break; case "folder": $filetype = "folder"; $actionfile = "<img src=\"img/folder.jpg\" height=\"130\" width=\"130\">"; $actionlink = "<a href=\"files.php?folder=$actfolder$file/\">"; break; default: $filetype = "other"; $actionfile = "<img src=\"img/unknown.jpg\" height=\"130\" width=\"130\">"; $actionlink = "<a href=\"view.php?folder=$actfolder&file=$file\">"; } if (strlen($file) <= 19) { $filestamp = "$file"; } else { $filestamp = "..." . substr("$file", -16); } if ((!is_dir($file)) & ($file != ".") & ($file != "..")) echo "<li class=\"$filetype\"> <div id='titleblock'>$actionlink<p>" . $filestamp . "</p></a></div> $actionlink$actionfile</a> <a href='remove.php?folder=$actfolder&file=$file' title='Delete file '$file' from the server'> <div id='removeblock'></div></a> <a href='#' data-dropdown='#dropdown-$uid'><div id='renameact'></div></a> <div id='dropdown-$uid' class='dropdown dropdown-tip'> <div class='dropdown-panel'> <form action='rename.php' method='POST'> <input type='text' name='filename' value='$file' style='display:none;' > <input type='text' name='newfilename' value='$file'> <button>Rename</button> </form> </div> </div> <a href='#' data-dropdown='#dropdown-$shareid'><div id='shareact'></div></a> <div id='dropdown-$shareid' class='dropdown dropdown-tip'> <div class='dropdown-panel'> <form> <input type='text' name='newfilename' value='$filelink'> <button>Rename</button> </form> </div> </div> <a href='#' data-dropdown='#dropdown-$moveid'><div id='renameact'></div></a> <div id='dropdown-$moveid' class='dropdown dropdown-tip'> <div class='dropdown-panel'> <form action='move.php' method='post'> <input type='text' name='filename' value='$file' onblur='if(value=='') value = '$file'' onfocus='if(value=='$file') value = '''> <input type='text' name='foldername' value='Folder' onfocus='if(value=='Folder') value = ''' onblur='if(value=='') value = 'Folder'' > <button>Move</button> </form> </div> </div> </li>"; } } closedir($directory_handle); } } ?> Edited March 1, 2014 by ArshSingh Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 1, 2014 Share Posted March 1, 2014 (edited) how do i glob search so will get result without typing extension of file Use the * character in the pattern to match anything, eg to find any files beginning with yo you'd use yo* as the pattern Edited March 1, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted March 1, 2014 Author Share Posted March 1, 2014 In pattern???? Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted March 1, 2014 Author Share Posted March 1, 2014 Made a try but not works Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 1, 2014 Share Posted March 1, 2014 In pattern???? Read the manual? http://php.net/glob $filesFound = glob('yo*'); // find any files beginning with yo printf('<pre>%s</pre>', print_r($filesFound, true)); Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted March 1, 2014 Author Share Posted March 1, 2014 Take a look at my current code which i posted ,,, is there anyway to modifie itand make it glob?? Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted March 1, 2014 Author Share Posted March 1, 2014 Yo* is not working on this code,,, any suggestion for modifie the code?? Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted March 1, 2014 Solution Share Posted March 1, 2014 (edited) You need to replace the while loop while (($file = readdir($directory_handle)) !== false) { ... } with something like $pattern = $directory . DIRECTORY_SEPARATOR . $query . '*'; echo 'Your search pattern ('.$pattern.') matches: '; foreach(glob($pattern) as $file) { // code to list/display files } Edited March 1, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.