如何用php搜索文件

big*_*jim 3 php search file file-search

第一件事是第一。我不是PHP开发人员,这是我的工作所需要的,所以我接手了,并且我在学习中

现在,我们有一个excel表格,其中包含我们制作的项目的手册链接,这些链接必须手动更新。这可能需要几个小时才能完成。因此,我试图找到一种方法来节省时间。

我可以阅读excel文件以使用javascript获取所需的信息,然后使用ajax调用将其发送到php。

我已确保获得所需的数据,并使其看起来如何在服务器上运行。

我整天都在搜寻Google,以使其正常工作,但我一直空着头。

这是我在php文件中的代码。

    <?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;
}
Run Code Online (Sandbox Code Playgroud)

所以我需要给php文件一个手册名,看看它是否在目录中。

该文件是searchManuals.php,存储在manuals文件夹(manuals / searchManuals.php)中。我要查找的文件位于与该文件位于同一目录中的文件夹(manuals / english / jdv0 / pdf / manual.pdf)。

VK3*_*321 5

尝试这个:

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

    }  
 } 
}
Run Code Online (Sandbox Code Playgroud)