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
or you can use
$_SERVER['DOCUMENT_ROOT'].'/adminpanel/hitcounter.txt';
Related
I have this code snippet:
<?php
$fh = fopen(__DIR__.'/foo', "w");
fwrite($fh, 'one');
rename(__DIR__ . '/foo', __DIR__ . '/bar');
fwrite($fh, 'two');
echo file_get_contents(__DIR__ . '/bar');
Output when I run this with php-7.1.26-nts-Win32-VC14-x64:
Warning: rename(C:\test/foo,C:\test/bar): The process cannot access the file because it is being used by another process. (code: 32) in C:\test\test.php on line 5
Warning: file_get_contents(C:\test/bar): failed to open stream: No such file or directory in C:\test\test.php on line 8
Output when I run this with php-7.2.15-nts-Win32-VC15-x64:
Warning: rename(C:\test/foo,C:\test/bar): The process cannot access the file because it is being used by another process. (code: 32) in C:\test\test.php on line 5
Warning: file_get_contents(C:\test/bar): failed to open stream: No such file or directory in C:\test\test.php on line 8
Output when I run this with php-7.3.1-nts-Win32-VC15-x64:
onetwo
I think the responsible code is here and here, but I can't identify a change that might be responsible for this behavior.
I just took like 45 minutes doing a tut for it not to work my code is here:
<?php
$path = dirname(__FILE__);
include("{$path}//config.inc.php");
include("{$path}//mc.inc.php");
?>
and this is what the webpage shows:
Warning: include(C:\xampp\htdocs\NationKong site\core//mc.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\NationKong site\core\init.inc.php on line 6
Warning: include(): Failed opening 'C:\xampp\htdocs\NationKong site\core//mc.inc.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\NationKong site\core\init.inc.php on line 6
$config
Server Status
you are on Windows, Directory separator is \, in a php string it would be "\\"
so you should write
<?php
$path = dirname(__FILE__);
include("{$path}\\config.inc.php");
include("{$path}\\mc.inc.php");
?>
if you like to make php to determine the right directory separator for you, just use DIRECTORY_SEPARATOR
<?php
$path = dirname(__FILE__);
include($path.DIRECTORY_SEPARATOR."config.inc.php");
include($path.DIRECTORY_SEPARATOR."mc.inc.php");
?>
I have a problem with a php code.
Folder name what-counter This folder contains a file with the following php code named counter.php also the hitcount.txt file.
<?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 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
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"); ?> to get it to work in files in root directory as well as folders?
Either:
Include relative the the file: __DIR__.'/../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.
Change the working directory to a known/static 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:
$filename = dirname(__FILE__) . '/what-counter/hitcount.txt';
$filename = dirname(dirname(__FILE__)) . '/what-counter/hitcount.txt';
According to fopen documentation, in some modes it will create the file if it does not exist, but in my situation, I've checked all 'w', 'w+', 'x' and 'x+' modes but it's just throwing warnings at me and it cannot create the file.
It's my code:
$this->handle = fopen($this->log_name, 'w');
and what I get:
Warning: fopen(D:\xampp\htdocs\farid\logs\error.php) [function.fopen]: failed to open stream: No such file or directory in D:\xampp\htdocs\farid\libraries\error\log.php on line 34
Warning: fwrite() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\farid\libraries\error\log.php on line 66
Warning: fclose() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\farid\libraries\error\log.php on line 27
And I'm working in windows environment.
Check if the path to the logfile exists, it creates a file, not a directory.
Also check if the user, which runs xampp, has access to the folder you specified.
$file = 'my/path/to/htaccess/location/.htaccess';
$htaccess = file($file);
$ht = fopen($htaccess,'a');
fwrite($ht,"deny");
fclose($ht);
I'm trying to modify the .htaccess file via functions.php, the CHMOD is set to 777, any ideas?
Edit:
Just enabled errors:
Warning: file() [function.file]: URL file-access is disabled in the server configuration in /home/tfbox/domains/ibrogram.com/public_html/themes/beta/wp-content/themes/beta/functions.php on line 133
Warning: file(http://themes.ibrogra.com/beta/.htaccess) [function.file]: failed to open stream: no suitable wrapper could be found in /home/tfbox/domains/ibrogram.com/public_html/themes/beta/wp-content/themes/beta/functions.php on line 133
Warning: fopen() [function.fopen]: Filename cannot be empty in /home/tfbox/domains/ibrogram.com/public_html/themes/beta/wp-content/themes/beta/functions.php on line 135
Warning: fwrite(): supplied argument is not a valid stream resource in /home/tfbox/domains/ibrogram.com/public_html/themes/beta/wp-content/themes/beta/functions.php on line 137
Warning: fclose(): supplied argument is not a valid stream resource in /home/tfbox/domains/ibrogram.com/public_html/themes/beta/wp-content/themes/beta/functions.php on line 139
This
$file = 'http://'.$_SERVER['SERVER_NAME'].'/beta/.htaccess';
makes the requested path a http path.
That doesn't make sense - you want to use a file path.
You could use
$file = $_SERVER['DOCUMENT_ROOT'].'/beta/.htaccess';
instead.