[英]PHP warning from root directory
我的php代碼有問題。
文件夾名稱 what-counter此文件夾包含一個文件,其中包含以下名為counter.php的php代碼以及hitcount.txt文件。
<?php
$filename = '../what-counter/hitcount.txt';
$handle = fopen($filename, 'r');
$hits = trim(fgets($handle)) + 1;
fclose($handle);
$handle = fopen($filename, 'w');
fwrite($handle, $hits);
fclose($handle);
// Uncomment the next line (remove //) to display the number of hits on your page.
echo $hits;
?>
以下php代碼用於根目錄文件中,也用於回顯命中的文件夾中的文件中。
<?php include("../what-counter/counter.php"); ?>
問題代碼在文件夾中的文件中起作用,但在直接位於根目錄中的文件中則無效。 示例:index.php在根目錄中
使用此代碼我得到此警告
<?php include("what-counter/counter.php"); ?>
Warning: fopen(../what-counter/hitcount.txt) [function.fopen]: failed to open stream: No such file or directory in /home/what/public_html/what-counter/counter.php on line 4
Warning: fgets(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 5
Warning: fclose(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 6
Warning: fopen(../what-counter/hitcount.txt) [function.fopen]: failed to open stream: No such file or directory in /home/what/public_html/what-counter/counter.php on line 8
Warning: fwrite(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 10
並使用此代碼我得到此警告
<?php include("../what-counter/counter.php"); ?>
Warning: include(../what-counter/counter.php) [function.include]: failed to open stream: No such file or directory in /home/what/public_html/include/footer.php on line 31
Warning: include(../what-counter/counter.php) [function.include]: failed to open stream: No such file or directory in /home/what/public_html/include/footer.php on line 31
Warning: include() [function.include]: Failed opening '../what-counter/counter.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/what/public_html
/include/footer.php on line 31
我可以對$ filename url和<?php include("../what-counter/counter.php"); ?>
<?php include("../what-counter/counter.php"); ?>
得到它在文件中工作,根目錄以及文件夾?
要么:
__DIR__.'/../somefile';
/ __DIR__.'/somepath/somefile';
$_SERVER['DOCUMENT_ROOT']
,以及引導程序代碼中類似於PROJECT_DIR
的define()
。 chdir('/some/dir');
。 不建議使用此代碼,因為調用代碼可能不會這樣。 聽起來像是絕對文件還是相對文件問題...
嘗試更換:
$filename = '../what-counter/hitcount.txt';
使用以下方法之一,取決於文件的確切位置:
$filename = __DIR__ . '/what-counter/hitcount.txt';
$filename = dirname(__DIR__) . '/what-counter/hitcount.txt';
或者(如果您使用的是舊版php):
$filename = dirname(__FILE__) . '/what-counter/hitcount.txt';
$filename = dirname(dirname(__FILE__)) . '/what-counter/hitcount.txt';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.