【问题标题】:file exists but fopen "failed to open stream no such file or directory"文件存在但 fopen “无法打开流,没有这样的文件或目录”
【发布时间】:2015-07-06 21:26:14
【问题描述】:

这是一个非常奇怪的问题。

$file = '/home/wwwroot/website/web/modules/homegrid/css/bootstrap-carousel.min.css';
clearstatcache();
var_dump(file_exists($file));//output true
var_dump(is_file($file));//output true
var_dump(is_dir($file));//output false
var_dump(is_readable($file));//output true
var_dump(is_writable($file));//output true
var_dump(fopen($file, 'w+'));//output false
var_dump(file_put_contents($file, 'asdfasdf'));//output false

fopen 和 file_put_contents 始终为 false,但文件存在。 在 php 5.6 和 php-fpm 中

【问题讨论】:

  • 是符号链接吗?
  • 不是链接,权限是-rwxrwxrwx
  • (fopen($file, 'r+')); 试试这个
  • 你能用ls -larth /home/wwwroot/website/web/modules/homegrid/css/bootstrap-carousel.min.css的结果编辑问题吗?它将显示有关该文件的一些有用信息。
  • 对不起,它仍然是 fopen(/home/wwwroot/website/web/modules/homegrid/css/bootstrap-carousel.min.css): 无法打开流: 没有这样的文件或目录跨度>

标签: php fopen


【解决方案1】:

8小时后终于找到了解决办法,但还是不知道为什么。

if(file_exists($file))
{
    unlink($file);
    touch($file);
}
var_dump(fopen($file, 'w+'));//output resource

就像是文件写入被阻塞了,我没听说php有文件阻塞的东西。

【讨论】:

    【解决方案2】:

    你好尝试检查最后一个错误

    $fp = fopen($file, 'w+');
    if ( !$fp ) {
      echo 'last error: ';
      var_dump(error_get_last());
    }
    else {
      echo "ok.\n";
    }
    

    【讨论】:

    • 您也可以使用fopen($file, 'w+') or die(error_get_last()); 表示法在这里杀死脚本并显示错误。
    • error_get_last 返回与此问题无关的错误:“ob_end_clean(): failed to delete buffer. No buffer to delete”
    • @znjack 查找之前的一些错误。同一个函数可以抛出多个错误
    【解决方案3】:
    <?php
        $myfile = fopen($file, "r+") or die("Unable to open file!");
    
        $txt = "asdfasdf";
        fwrite($myfile, $txt);
    
        fclose($myfile);
        ob_end_clean();
    ?>
    

    (fopen($file, 'w+')); 更改为(fopen($file, 'r+'));

    文件权限

    1. r+ 打开文件进行读/写。文件指针从 文件开头
    2. w+ 打开文件进行读/写。 擦除文件的内容 或者如果它不存在则创建一个新文件。文件指针开始于 文件的开头

    【讨论】:

    • 我试过r+,还是“打开流失败:没有这样的文件或目录”,好像文件夹不存在,但是file_exists等函数返回true。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2022-06-19
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多