im making a photo gallery and i want to make a search bar which displays all the images that have the searched keyword in their name. i have stored my images in a folder (NOT DATABASE, IN A FOLDER). i have made the search bar but i cant get the PHP part working. do i need ajax or jquery ? this is my current code which returns nothing when i press search. not even the test " eco 'hello' " part.
?php
$dir = "/uploads";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == $_POST['searching']){
// replace this line with eco <img> line
echo('<a href="'.$dir . $file.'">'. $file .'</a>'."
");
echo "hello" ;
}
}
closedir($dh);
}
}
?>
i want to get a list of the images with keyword (searching) in the name.
It could be as simple as replacing if($file == $_POST['searching']){
with if (false !== strpos($file, $_POST['searching'])){
.
You could use scandir()
to get an array of files in the directory and then loop through it:
$dir_array=scandir($dir);
foreach($dir_array as $file){
if($file == $_POST['searching']){
// open file etc. here
echo('<a href="'.$dir . $file.'">'. $file .'</a>'."
");
}
}