首页 > 解决方案 > fopen 无法在没有错误的服务器上运行

问题描述

我的代码在 Windows 环境中的本地 PHP 实例(版本 5.4.31)上运行良好。

但是当我在我的服务器上运行同一个文件时,它在 Centos 环境中的服务器(PHP 版本 5.4.16)上不起作用。

我的PHP代码:

<?php
//$myfile = fopen("testfile.txt", "w");

if (!file_exists("file.txt")) { 
  die('File does not exist'); 
}

clearstatcache(); 
// $fh = fopen("file.txt", "r") or die("can't open file");

$fh = fopen("file.txt", "r") or die($php_errormsg); 
$sql = fread($fh, filesize("file.txt")); 

error_reporting(E_ALL);
ini_set('display_errors',1);

?>

问题是文件没有生成,我无法看到错误日志,我在哪里可以看到错误,以便我可以找到相应的解决方案?

标签: php

解决方案


通常是这样的:

<?php
 error_reporting(E_ALL);
ini_set('display_errors',1);

//$myfile = fopen("testfile.txt", "w");
if (!file_exists("file.txt")) { die('File does not exist'); }
 clearstatcache(); 
// $fh = fopen("file.txt", "r") or die("can't open file");
$fh = fopen("file.txt", "r") or die($php_errormsg); 
 $sql = fread($fh,filesize("file.txt")); 

?>

file.txt应该在同一个文件夹中

┬root
  |
  |
  └─┬www
    |
    ├─ test4.php
    | 
    ├─ file.txt
    | 

推荐阅读