>问题 1:我得到了这个代码
<?php
$count_my_page = ("adminpanel/hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
?>
它在一个名为 /beta/
的文件中,但我需要它来查找文件夹/adminpanel/
我尝试使用 /../
并//
它不起作用。我创建了错误报告文件,这是输出:
Warning: file(//adminpanel/hitcounter.txt): failed to open stream: No such file or directory in /home/u463251352/public_html/beta/index.php on line 3
Warning: fopen(//adminpanel/hitcounter.txt): failed to open stream: No such file or directory in /home/u463251352/public_html/beta/index.php on line 5
Warning: fputs() expects parameter 1 to be resource, boolean given in /home/u463251352/public_html/beta/index.php on line 6
Warning: fclose() expects parameter 1 to be resource, boolean given in /home/u463251352/public_html/beta/index.php on line 7
我该如何解决这个问题?
您应该使用基于魔术常量的绝对路径。
对于 5.3.0 或更高版本,您可以使用:
__DIR__
对于低于 5.3.0 的 PHP,请使用:
dirname(__FILE__)
例:
$file = __DIR__ . '/dir/file.txt';
将采取:
- 根
- 迪尔
- 文件.txt
- 迪尔
或者你可以使用
$_SERVER['DOCUMENT_ROOT'].'/adminpanel/hitcounter.txt';