文件存在但fopen"无法打开流没有这样的文件或目录"

时间:2015-07-06 10:08:33

标签: php fopen

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

$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

3 个答案:

答案 0 :(得分:2)

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

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

它就像阻止文件写入的东西一样,我从未听说过php有任何文件阻塞的东西。

答案 1 :(得分:1)

嗨,请尝试检查上一个错误

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

答案 2 :(得分:0)

ActionBar

<?php $myfile = fopen($file, "r+") or die("Unable to open file!"); $txt = "asdfasdf"; fwrite($myfile, $txt); fclose($myfile); ob_end_clean(); ?> 更改为此(fopen($file, 'w+'));

文件权限

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