文件存在但 fopen "failed to open stream no such file or directory"

file exists but fopen "failed to open stream no such file or directory"

真是个奇怪的问题。

$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

你好 请检查最后一个错误

$fp = fopen($file, 'w+');
if ( !$fp ) {
  echo 'last error: ';
  var_dump(error_get_last());
}
else {
  echo "ok.\n";
}
<?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+ 打开 read/write 的文件。 文件指针开始于 文件开头
  2. w+ 打开 read/write 的文件。 擦除文件的内容 或者创建一个新文件(如果不存在)。文件指针开始于 文件开头

8小时后我终于找到了解决方法,但仍然不知道为什么。

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

好像有什么东西在阻止文件写入,我从来没听说过php有任何文件阻塞的东西。