how to search for a file with php

22,816

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);

    }  
 } 
}
Share:
22,816
bigjim
Author by

bigjim

Updated on July 09, 2022

Comments

  • bigjim
    bigjim almost 2 years

    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).

  • bigjim
    bigjim almost 7 years
    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 there
  • bigjim
    bigjim almost 7 years
    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.
  • bigjim
    bigjim almost 7 years
    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-2017
  • bigjim
    bigjim almost 7 years
    That works for small scale testing. I spent hours trying to figure this out and you did it in minutes. Thank you very much