I have some doubts in creating search engine to find directory path of file.
For example, Consider this is updated code,
<?php
$files = array();
if(isset($_POST['submit'])) {
$file_to_search = $_POST['file'];
$root_path = "C:\\xampp\\htdocs\\cat\\"; // your doc root
foreach ($iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root_path,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $value) {
if($value->getFilename() == $file_to_search) {
$files[] = $value->getPathname();
}
}
}
?>
<form method="POST" action="index1.php">
File To Search: <input type="text" name="file" /><br/>
<input type="submit" name="submit" value="Search" />
</form>
<?php if(!empty($files)): ?>
<div id="output">
<?php foreach($files as $value) {
echo "$value<br/>";
} ?>
</div>
<?php endif; ?>
I want to create search engine, which is use to find the exact directory path of certain file.
Can anyone help me? Thanks in advance.
As to your question of getting the exact directory path if the certain file, alternatively, you could use a RecursiveDirectoryIterator
to search for that file that you want to know where it is inside.
Lets say, I have a structure like this:
/var/
/www/
/test/
/images/
/images1/
image.png <--- this one for example
image.jpg
/images2/
image3.png
Lets say you want to search image.png. Consider this example:
$file_to_search = 'image.png';
$root_path = '/var/www/test'; // your doc root
foreach ($iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root_path,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $value) {
if($value->getFilename() == $file_to_search) {
echo $value->getPathname();
}
}
// should output: /var/www/test/images/images1/image.png
Simple Application of the code above:
<?php
$files = array();
if(isset($_POST['submit'])) {
$file_to_search = $_POST['file'];
$root_path = '/var/www/test'; // your doc root
foreach ($iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root_path,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $value) {
if($value->getFilename() == $file_to_search) {
$files[] = $value->getPathname();
}
}
}
?>
<form method="POST" action="index.php">
File To Search: <input type="text" name="file" /><br/>
<input type="submit" name="submit" value="Search" />
</form>
<?php if(!empty($files)): ?>
<div id="output">
<?php foreach($files as $value) {
echo "$value<br/>";
} ?>
</div>
<?php else: ?>
<?php if(isset($_POST['submit'])) echo 'no file found'; ?>
<?php endif; ?>