如何使用 php 搜索文件
how to search for a file with php
第一件事就是第一。我不是 php 开发人员,这是我工作所需要的东西,所以我接受了它,并且边走边学
现在我们有一个 excel sheet,其中包含我们制作的项目的手册链接,这些链接必须手动更新。这可能需要几个小时才能完成。所以我想找到一种方法来缩短时间。
我可以使用 javascript 读取 excel 文件以获取我需要的信息,然后通过 ajax 调用将其发送到 php。
我已经确定我得到了我需要的数据,并让它看起来像他们在服务器上的表现。
我整天都在谷歌上搜索,试图让它工作,但我总是一无所获。
这是我在 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;
}
所以我需要给 php 文件一个手册的名称,看看它是否在某个目录中。
这个文件searchManuals.php存放在manuals文件夹(manuals/searchManuals.php)。我找的文件都在和它同目录的文件夹里(manuals/english/jdv0/pdf/manual.pdf).
试试这个:
$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);
}
}
}
glob()函数怎么样?
PHP Docs
受VK321回答的启发,这里是另一个提前终止的版本:
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);
第一件事就是第一。我不是 php 开发人员,这是我工作所需要的东西,所以我接受了它,并且边走边学
现在我们有一个 excel sheet,其中包含我们制作的项目的手册链接,这些链接必须手动更新。这可能需要几个小时才能完成。所以我想找到一种方法来缩短时间。
我可以使用 javascript 读取 excel 文件以获取我需要的信息,然后通过 ajax 调用将其发送到 php。
我已经确定我得到了我需要的数据,并让它看起来像他们在服务器上的表现。
我整天都在谷歌上搜索,试图让它工作,但我总是一无所获。
这是我在 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;
}
所以我需要给 php 文件一个手册的名称,看看它是否在某个目录中。
这个文件searchManuals.php存放在manuals文件夹(manuals/searchManuals.php)。我找的文件都在和它同目录的文件夹里(manuals/english/jdv0/pdf/manual.pdf).
试试这个:
$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);
}
}
}
glob()函数怎么样? PHP Docs
受VK321回答的启发,这里是另一个提前终止的版本:
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);