Jump to content

Do Glob search for files


ArshSingh
Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by ArshSingh
Link to comment
Share on other sites

 

 

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 by Ch0cu3r
Link to comment
Share on other sites

  • Solution

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.