开发者

php cli reading files and how to fix it?

While using PHP cli in ubuntu i got apache as web server i cant read the file which i can while using it in the browser. And about the error it just shows nothing and the next like i just used this code for my testing purposes :

<?
$handle = fopen("testFile.txt", "r");
if ($handle) {
    while ((开发者_StackOverflow社区$buffer = fgets($handle, 4096)) !== false) {
    echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
if(!is_readable($handle)
{
 die("Not readable");
}
fclose($handle);
}
?>

How do i fix that ?

EDIT :

After removing the '@' before fopen i got the following error

fopen(testFile.txt): failed to open stream: No such file or directory in /var/www/log/ka.php on line 2


When you run your script through CLI, it uses your shell's working directory, in opposition to the file's directory (which Apache hands as the current directory). This is important for you, since your fopen call depends on a relative path; and relative paths are resolved relatively to the working directory, not to the script.

To have your script behave like it does with Apache, you either need to cd /var/www/log prior to running the php command, or add this at the beginning of your PHP script:

chdir(dirname(__FILE__));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜