php cli reading files and how to fix it? -
while using php cli in ubuntu got apache web server cant read file can while using in browser. , error shows nothing , next used code testing purposes :
<? $handle = fopen("testfile.txt", "r"); if ($handle) { while (($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 fix ?
edit :
after removing '@' before fopen got following error
fopen(testfile.txt): failed open stream: no such file or directory in /var/www/log/ka.php on line 2
when run script through cli, uses shell's working directory, in opposition file's directory (which apache hands current directory). important you, since fopen
call depends on relative path; , relative paths resolved relatively working directory, not script.
to have script behave apache, either need cd /var/www/log
prior running php
command, or add @ beginning of php script:
chdir(dirname(__file__));
Comments
Post a Comment