how to search for a file with php
Asked Answered
G

3

6

First thing is first. I am not a php developer this is something that is needed for my job so I took it on and I am learning as i go

Right now we have an excel sheet that holds links for a manuals for the items we make and these have to be updated manually. It can take hours to do. so I am trying to find a way to do this to cut the time.

I can read the excel file to get the info I need using javascript and then I send that to php with an ajax call.

I have made sure I get the data I need and make it look how they do on the server.

I have been googling all day trying to get it to work but I just keep coming up empty.

Here is my code in the php file.

    <?php
$search = isset($_POST['urlData']) ? rawurldecode($_POST['urlData']) : "Nope Still not set";
$path = $_SERVER['DOCUMENT_ROOT'];


$it = new RecursiveDirectoryIterator( $path );
foreach (new RecursiveIteratorIterator($it) as $file){
    $pathfile = str_replace($path,'',$file);
    if (strpos($pathfile, $search) !== false) {
        echo " pathFile var => ". $pathfile . "| Search var => " . $search;
        $encodedUrl = rawurlencode($pathfile .$search);
        echo 'link = http://manuals.myCompany.com/'. $doneUrl .'<br>';

    }else{

        echo "File does not exist => ";
        echo $path. "<= Path " . $search."<= Search ". $pathfile . "<= Pathfile";

    }
    break;
}

So I need to give the php file the name of a manual and see if it is in the directory somewhere.

this file is searchManuals.php stored in the manuals folder (manuals/searchManuals.php).The files I look for are in folders in the same directory with it (manuals/english/jdv0/pdf/manual.pdf).

Gruesome answered 1/6, 2017 at 19:29 Comment(5)
your post is very confusing... are you looking for function to search files in any specific directory using php?Indisputable
Sorry. I need to put in a manual name and find it on the server in one of the many files that hold them. I only get the name. and need to return the link to that fileGruesome
Why don't you use this amazing library? symfony.com/doc/current/components/finder.html The first example actually searches a directory recursivelyEwers
Sorry it is very confusing ill rewordGruesome
ok I cut out a bunch is this better?Gruesome
I
10

Try this:

$file_to_search = "abc.pdf";

search_file('.',$file_to_search);




function search_file($dir,$file_to_search){

$files = scandir($dir);

foreach($files as $key => $value){

    $path = realpath($dir.DIRECTORY_SEPARATOR.$value);

    if(!is_dir($path)) {

        if($file_to_search == $value){
            echo "file found<br>";
            echo $path;
            break;
        }

    } else if($value != "." && $value != "..") {

        search_file($path, $file_to_search);

    }  
 } 
}
Indisputable answered 1/6, 2017 at 19:43 Comment(4)
I am testing it now. but i am testing on my local with wamp. I just copied over the files via ftp to keep it the same. It is not finding what I am looking for but they are files I have made sure are thereGruesome
Ok I tried it they way you have this set up and I also added DIR in the scandir function to see if that would work but neither seem to make a difference.Gruesome
There should never be more than one with the same name. they all have dates attached to the end of the name so it might be a manual03-04-2017Gruesome
That works for small scale testing. I spent hours trying to figure this out and you did it in minutes. Thank you very muchGruesome
H
1

How about glob() function? PHP Docs

Heteroclite answered 20/2, 2018 at 15:0 Comment(0)
U
1

Inspired by VK321's answer, here is another version with early termination:

class Test
{
    public static function find($dir, $targetFile)
    {
        $filePath = null;

        // pass function ref &$search so we can make recursive call
        // pass &$filePath ref so we can get rid of it as class param
        $search = function ($dir, $targetFile) use (&$search, &$filePath) {
            if (null !== $filePath) return; // early termination
            $files = scandir($dir);
            foreach ($files as $key => $file) {
                if ($file == "." || $file == "..") continue;
                $path = realpath($dir . DIRECTORY_SEPARATOR . $file);
                if (is_file($path)) {
                    if ($file === $targetFile) {
                        $filePath = $path;
                        break;
                    }
                } else {
                    $search($path, $targetFile);
                }
            }
        };

        $search($dir, $targetFile); // invoke the function

        return $filePath;
    }
}

// $dir = './', $targetFile = "Foo.php";
echo Test::find($dir, $targetFile);
Ulrica answered 23/4, 2022 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.