【问题标题】:PHP - file_get_contents failed to open stream: No such file or directory even when file path is correctPHP - file_get_contents 无法打开流:即使文件路径正确,也没有这样的文件或目录
【发布时间】:2020-12-24 09:59:39
【问题描述】:

我正在尝试使用 PHP 获取文件的内容。然后将其添加到新文件中。我正在使用file_get_contents(),但它显示错误:

PHP Warning:  file_get_contents(./home-starter.php): failed to open stream: No such file or directory

即使路径是正确的。两个文件都在同一个文件夹中。

Main Folder
   errorfile.php
   home-starter.php

有什么建议吗?

<?php

require_once('private/initialize.php');

$city = 'Lahore';
$keyword = 'Homemade Food';
$areas = ['DHA', 'Bahria Town', 'Cantt', 'Gulberg', 'Valencia Town', 'Sui Gas Housing Society', 'Izmir Town', 'Faisal Town', 'Garden Town', 'Johar Town', 'Punjab Housing Society', 'Canal Housing Societies', 'State Life Housing Society', 'Model Town', 'Liaqatabad', 'Rehmanpura', 'Samanabad', 'Muslim Town', 'Town Ship', 'Iqbal Town', 'Ferozepure Road', 'Multan Road', 'Baghbanpura', 'Mughalpura', 'Walton'];

foreach ($areas as $area) {
  $permalink = strtolower(str_replace(' ', '-', $keyword)) . '-' . strtolower(str_replace(' ', '-', $area)) . '-' . strtolower($city);
  $page_title = 'Homemade Food Delivery - Lunch Dinner - ' . $area . ' - ' . $city;
  $meta_desc = 'Lunch.pk is Pakistan #1 website to order homemade food in ' . $area . ' ' . $city . '. You get tasty and healthy food cooked and delivered by families near you.';
  $location = $area . ' ' . $city;
  $filename = $permalink . '.php';

  $content = file_get_contents( __DIR__ . '/home-starter.php');
  $content = str_replace('[page-title]', $page_title, $content);
  $content = str_replace('[meta-desc]', $meta_desc, $content);
  $content = str_replace('[location]', $location, $content);

  $is_file_created = file_put_contents($filename, $content);

  if (!$is_file_created) {
    echo 'Err';
    break;
  }
}

【问题讨论】:

  • 试试file_get_contents(__DIR__ . '/home-starter.php'),看看有什么不同。
  • @MagnusEriksson 还是一样!
  • 在此处粘贴实际代码。可能是错字什么的。
  • 如果你回显getcwd()怎么办?这是正确的路径吗?
  • @Michel 我已经复制了它打印的路径但仍然是同样的错误:(

标签: php file-get-contents


【解决方案1】:

如果存在则检查文件


$file=__DIR__ . '/home-starter.php'; 
if(file_exists($file))
{
   $content=file_get_contents($file);
}

找不到正确路径,使用getcwd()函数获取当前工作目录

例如,您的项目具有这种结构


  • 文件夹
    • 应用程序
    • 型号 -> 模型.php
  • 其他文件夹
  • 配置->config.php

假设你会在任何地方得到 config.php 的内容,你应该使用下面的代码


$content=file_get_contents(getcwd().'/config/config.php');

【讨论】:

    【解决方案2】:

    您需要使用物理网址

    ./home-starter.php

    例如file_get_contents('C:\xampp\htdocs\project1\home-starter.php');

    【讨论】:

      【解决方案3】:

      我在本地开发机器上成功运行脚本后实施 cronjob 时遇到了这个问题。

      我的解决方案是使用 file_get_contents 的第二个函数参数将 $use_include_path 设置为 TRUE

      $currentFile = file_get_contents($fileinfo->getFilename(),true);
      

      See the function's docs at php.net

      【讨论】:

        【解决方案4】:

        检查您的 PHP 版本是否支持__DIR__,我能够通过 PHP 7.4.24 上非常相似的方法获取文件内容,但我使用了一个返回内容的函数。

        $content = get_file_data("myfile.txt");
        
        function get_file_data($filename) {
            $data = file_get_contents(__DIR__ . "/$filename");
        
            return $data;
        }
        
        

        【讨论】:

        • 用户在一年前已经在 cmets 中声明 __DIR__ 不起作用,可能是因为权限问题。这个神奇的常数已经使用了将近 15 年,没有人使用不支持它的 PHP 版本。
        猜你喜欢
        • 1970-01-01
        • 2015-10-22
        • 2017-10-24
        • 2016-12-20
        • 2015-11-13
        • 2016-08-03
        相关资源
        最近更新 更多