Jump to content

[SOLVED] php file search


Recommended Posts

ohk so after 2 days of coding i just cant get it to work, im trying to make a search which displays files in a directory with a filter. eg: you search " gun ", the php looks for all file names with " gun " in it.

 

also the results can only show .ipa files and in a specific directory and if possible can all the coding be on one .php sheet.

 

i'm assuming this must be quite simple.

Link to comment
https://forums.phpfreaks.com/topic/157463-solved-php-file-search/
Share on other sites

thanks, its told me quite abit you cant just explain.

 

what about something that searches the words in a script eg:

 

i have a index.php file which displays whats in the directory its located in:

 

http://teambrod.110mb.com/upload/

 

now how about i have a search at the top, could i make it so it finds all the matches to:

 

"app 01.ipa""app 02"

Since you haven't shown any code, I can only guess. But if you're using glob() to return an array with the files, you can search them like this (had the script lying around anyway):

 

<?php
if ($_GET['name'] != '') {
$files = glob('directory/to/your/files/*.ipa');
$results = array();
foreach ($files as $file) {
	if (strpos(basename($file), $_GET['name']) !== false) {
		$results[] = $file;
	}
}
if (count($results) > 0) {
	echo 'The following files matched the search <em>' . htmlentities($_GET['name']) . "</em>:<br /><br />\n";
	foreach ($results as $file) {
		echo '<a href="' . $file . '">' . basename($file) . '</a><br />' . "\n";
	}
} else {
	echo 'No files matched the search <em>' . htmlentities($_GET['name']) . "</em>.<br />\n";
}
} else {
echo 'Search the files:<br />';
}
?>
<br />
<form action="index.php" method="get">
<input type="text" name="name" />
<input type="submit" />
</form>

Just remember to change directory/to/your/files/ and index.php to your actual directory and script. And if you want to make the search case-insensitive, use stripos() instead of strpos().

Since you haven't shown any code, I can only guess. But if you're using glob() to return an array with the files, you can search them like this (had the script lying around anyway):

 

<?php
if ($_GET['name'] != '') {
$files = glob('directory/to/your/files/*.ipa');
$results = array();
foreach ($files as $file) {
	if (strpos(basename($file), $_GET['name']) !== false) {
		$results[] = $file;
	}
}
if (count($results) > 0) {
	echo 'The following files matched the search <em>' . htmlentities($_GET['name']) . "</em>:<br /><br />\n";
	foreach ($results as $file) {
		echo '<a href="' . $file . '">' . basename($file) . '</a><br />' . "\n";
	}
} else {
	echo 'No files matched the search <em>' . htmlentities($_GET['name']) . "</em>.<br />\n";
}
} else {
echo 'Search the files:<br />';
}
?>
<br />
<form action="index.php" method="get">
<input type="text" name="name" />
<input type="submit" />
</form>

Just remember to change directory/to/your/files/ and index.php to your actual directory and script. And if you want to make the search case-insensitive, use stripos() instead of strpos().

 

yeah mate, thats very very close to perfect. i tweaked it a little and wahlah all good, you want any credits on the site or script?(i basically did copy and paste the script, only minor changes)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.