I have a php-script to search in my webspace-files. I can toggle (case) if I want to search case-sensitive.
It function if I toggle case-sensitive. But when deselect this option I get no result if my word is in upper-case. So I think that the script lowers every time. But I can’t find, even now after hours, what is wrong…
PHP 8.3
The script gives me results. So if you try on the website linked above with the german Geist (=ghost) it gives me with toggled on "case-sensitive" 138 hits. But with toggled off it gives me "Sorry No Result Found." even so the result must have been 159. So I think there must be a fault with the strtolower-command?
Any idea?
If I insert the whole script, I dont get an error in the php-sandbox on [PHP Sandbox][1] here
s the the part of script which handles "case":
<?php
$count = 0;
function getDirContents($dir, &$results = array()) {
global $count;
$searchStrings = array();
if(isset($_POST['searchStrings']))
$searchStrings = json_decode($_POST['searchStrings'], true);
if ($_POST['search'] == null) { echo "1"; exit;}
ini_set('max_execution_time', $_POST['maxtime']);
$_SESSION['searchString'] = $_POST['search'];
echo "<script>var elm = document.getElementById('search');elm.value='$_POST[search]';</script>";
if (!isset($_POST['case']))
$string = strtolower($_POST['search'] ?? '');
else
$string = $_POST['search'];
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
if (!isset($_POST['case']))
$content = strtolower(file_get_contents($path));
else
$content = file_get_contents($path);
$label = null;
$fileName = basename($path, '?' . $_SERVER['QUERY_STRING']);
$type = null;
$types = array();...
So it must be an error, that the PHP-Sandbox can`t find…
2
Answers
When you want to compare to strings in a case-insensitive way you have to convert both strings to lower (or upper).
As your code is incomplete and in the shown part only one of strings is converted to lower case, I think you have forgot to convert the other string to lower.
For simple search maybe you can do something like this
Solution with count handling
Warning
Here is an XSS security issue
if
$_POST[search]
contains javascript, it is possible to do bad things