【问题标题】:Failed to open stream: No such file or directory with php - scandir无法打开流:没有这样的文件或目录与 php - scandir
【发布时间】:2018-12-13 05:11:19
【问题描述】:

我正在使用 Laravel 5.1 在 /public/download 文件夹中,我需要获取许多 .htm 页面,所以我写了:

public function scraper() {

 $path = 'downloads/';
$files = scandir($path);
foreach ($files as $k => $file) {
    if ($k < 2) continue;
  $html = file_get_contents($file);

//parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("//div[@class='merchant-links']/a/@href");

echo $hrefs;
}
}

但我得到了错误:

AdminController.php 第 133 行中的 ErrorException: file_get_contents(1742-wine-bar-1.htm):打开流失败:没有这样的文件或目录 我也试试:

 $path = 'C:/wamp/www/mavis/public/downloads/';

【问题讨论】:

    标签: php laravel file-get-contents scandir


    【解决方案1】:

    我认为你应该使用

    $html = file_get_contents($path.$file);
    

    因为在 $html 变量中你只有一个文件名,但你必须在函数file_get_contents() 中包含一个目录路径

    【讨论】:

      【解决方案2】:

      我解决了我的问题:

      $files = glob('downloads/*.{htm}', GLOB_BRACE);
      

      【讨论】:

        猜你喜欢
        • 2015-11-13
        • 2016-08-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-11
        相关资源
        最近更新 更多