简体   繁体   中英

PHP Can't find file

Problem 1: I got this code

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

Its in a file called /beta/ but i need it to find the folder /adminpanel/ i tried using /../ and // it did not work. I created the error reporting file and this is the output:

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

How do i fix this?

You should use an absolute path based on magic constants .

For 5.3.0 or higher, you can use:

__DIR__

For PHP lower than 5.3.0, use:

dirname(__FILE__)

Example:

$file = __DIR__ . '/dir/file.txt';

Will take:

  • Root
    • dir
      • file.txt

或者你可以使用

$_SERVER['DOCUMENT_ROOT'].'/adminpanel/hitcounter.txt';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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