简体   繁体   English

来自根目录的PHP警告

[英]PHP warning from root directory

I have a problem with a php code. 我的php代码有问题。

Folder name what-counter This folder contains a file with the following php code named counter.php also the hitcount.txt file. 文件夹名称 what-c​​ounter此文件夹包含一个文件,其中包含以下名为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;

?>

The following php code is used in root directory files also in files from folders which echo's the hits. 以下php代码用于根目录文件中,也用于回显命中的文件夹中的文件中。

<?php include("../what-counter/counter.php"); ?>

The problem The code works in the files in folders but not in the files that are directly in the root directory. 问题代码在文件夹中的文件中起作用,但在直接位于根目录中的文件中则无效。 Example: index.php is in the root directory 示例:index.php在根目录中

Using this code i get this Warning 使用此代码我得到此警告

<?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

And using this code i get this Warning 并使用此代码我得到此警告

<?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

What can i do to the $filename url and <?php include("../what-counter/counter.php"); ?> 我可以对$ filename url和<?php include("../what-counter/counter.php"); ?> <?php include("../what-counter/counter.php"); ?> to get it to work in files in root directory as well as folders? <?php include("../what-counter/counter.php"); ?>得到它在文件中工作,根目录以及文件夹?

Either: 要么:

  • Include relative the the file: __DIR__.'/../somefile'; 包括相对文件: __DIR__.'/../somefile'; / __DIR__.'/somepath/somefile'; / __DIR__.'/somepath/somefile';
  • Include relative to a known dir: $_SERVER['DOCUMENT_ROOT'] is often used, as is a PROJECT_DIR -like define() in your bootstrap code. 相对于已知目录的包含:通常使用$_SERVER['DOCUMENT_ROOT'] ,以及引导程序代码中类似于PROJECT_DIRdefine()
  • Change the working directory to a known/static dir chdir('/some/dir'); 将工作目录更改为已知/静态目录chdir('/some/dir'); . Not recommended as calling code may not expect this. 不建议使用此代码,因为调用代码可能不会这样。

Sounds like absolute vs relative file problems... 听起来像是绝对文件还是相对文件问题...

Try replacing: 尝试更换:

$filename = '../what-counter/hitcount.txt';

With, depending on where the file is located exactly, either of: 使用以下方法之一,取决于文件的确切位置:

$filename = __DIR__ . '/what-counter/hitcount.txt';
$filename = dirname(__DIR__) . '/what-counter/hitcount.txt';

Or (if you're on an old php install) either of: 或者(如果您使用的是旧版php):

$filename = dirname(__FILE__) . '/what-counter/hitcount.txt';
$filename = dirname(dirname(__FILE__)) . '/what-counter/hitcount.txt';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM