来自根目录的PHP警告
我有一个php代码的问题 文件夹名称什么计数器此文件夹包含一个名为counter.php的php代码文件以及hitcount.txt文件来自根目录的PHP警告,php,Php,我有一个php代码的问题 文件夹名称什么计数器此文件夹包含一个名为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($h
<?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
警告:fopen(../what counter/hitcount.txt)[function.fopen]:无法打开流:第4行的/home/what/public\u html/what counter/counter.php中没有此类文件或目录
警告:fgets():在第5行的/home/what/public_html/what counter/counter.php中,提供的参数不是有效的流资源
警告:fclose():在第6行的/home/what/public\u html/what counter/counter.php中,提供的参数不是有效的流资源
警告:fopen(../what counter/hitcount.txt)[function.fopen]:无法打开流:第8行的/home/what/public\u html/what counter/counter.php中没有此类文件或目录
警告:fwrite():在第9行的/home/what/public_html/what counter/counter.php中,提供的参数不是有效的流资源
警告:fclose():在第10行的/home/what/public\u html/what counter/counter.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
警告:include(../what counter/counter.php)[function.include]:无法打开流:第31行的/home/what/public\u html/include/footer.php中没有此类文件或目录
警告:include(../what counter/counter.php)[function.include]:无法打开流:第31行的/home/what/public\u html/include/footer.php中没有此类文件或目录
警告:include()[function.include]:在/home/what/public\u html中打开“../what counter/counter.php”以包含(include_path=”:/usr/lib/php:/usr/local/lib/php)失败
/第31行包含/footer.php
要使$filename url和
在根目录和文件夹中的文件中工作,我可以对其执行什么操作?或者:
- 包括文件的相对值:
\uuuuu DIR\uuuuu./../somefile'代码>/
\uuuu DIR\uuuuu.'/somepath/somefile'代码>
- 相对于已知目录包含:
经常使用,就像引导代码中的$\u服务器['DOCUMENT\u ROOT']
项目目录一样
- 将工作目录更改为已知的/static dir
chdir('/some/dir')代码>。不建议这样做,因为调用代码可能不希望这样
$filename = '../what-counter/hitcount.txt';
使用,根据文件的确切位置,可以选择以下任一选项:
$filename = __DIR__ . '/what-counter/hitcount.txt';
$filename = dirname(__DIR__) . '/what-counter/hitcount.txt';
$filename = dirname(__FILE__) . '/what-counter/hitcount.txt';
$filename = dirname(dirname(__FILE__)) . '/what-counter/hitcount.txt';
或者(如果您使用的是旧的php安装),可以选择:
$filename = __DIR__ . '/what-counter/hitcount.txt';
$filename = dirname(__DIR__) . '/what-counter/hitcount.txt';
$filename = dirname(__FILE__) . '/what-counter/hitcount.txt';
$filename = dirname(dirname(__FILE__)) . '/what-counter/hitcount.txt';
如果我从文件中删除../并将其包含在index.php页面上,但不在文件夹文件中,则与..有关/