-2

So I have a URL which contains &title=blabla

I know how to extract the title, and return it. But I've been searching my ass off to get the full path to the filename when I only have the filename.

So what I must have is an way to search in all directories for an html file called 'blabla' when the only thing it has is blabla. After finding it, it must return the full path.

Anyone who does have an solution for me?

<?php

$file = $_GET['title'];

if ($title = '') {
echo "information.html";
} else { 
//here it must search for the filepath and echo it.
echo "$filepath";
}

?>
user2012141
  • 23
  • 1
  • 6
  • 1
    add examples... what you have and what you want... – Naz Feb 17 '13 at 02:30
  • One Trick Pony's solution should work for this, just set the `$root` variable to the folder containing your content pages to avoid searching unnecessary folders. Otherwise as mentioned, it will be very unresponsive. – PassKit Feb 17 '13 at 02:35

3 Answers3

0
$root = '/';               // directory from where to start search
$toSearch = 'file.blah';   // basename of the file you wish to search

$it = new RecursiveDirectoryIterator($root);
foreach(new RecursiveIteratorIterator($it) as $file){
  if($file->getBasename() === $toSearch){
    printf("Found it! It's %s", $file->getRealPath());

    // stop at the first match
    break;
  }
}

Keep in mind that depending on the number of files you have, this can be slow as hell

nice ass
  • 16,471
  • 7
  • 50
  • 89
  • Would it be faster to create a .txt that contains all the paths and just search if an line contains the filename, if so echo that line? – user2012141 Feb 17 '13 at 02:35
  • If that list is created once, yes. I guess you could rebuild the list every time the modification date of the `$root` directory changes – nice ass Feb 17 '13 at 02:37
0

You can use the solution provided here.

It allows you to recurse through a directory and list all files in the directory and sub-directories. You can then compare to see if it matches the files you are looking for.

Community
  • 1
  • 1
karancan
  • 2,152
  • 4
  • 23
  • 35
0

For a start this line is at fault

if ($title = '') {

See http://www.php.net/manual/en/reserved.variables.files.php

Ed Heal
  • 59,252
  • 17
  • 87
  • 127