PHP warning from root directory - 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';

Related

iam getting errors as follow Warning: include_once(C:/xampp/htdocs/timetable/header.php): failed to open stream: No such file or directory

Warning: include_once(C:/xampp/htdocs/timetable/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 4
Warning: include_once(): Failed opening 'C:/xampp/htdocs/timetable/header.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 4
Warning: include_once(C:/xampp/htdocs/timetable/footer.php): failed to open stream: No such file or directory in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 80
Warning: include_once(): Failed opening 'C:/xampp/htdocs/timetable/footer.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\timetablegeneratormaster\timetable\index.php on line 80
When including files that are in the same directory or to several levels higher or lower than the current file, use the __DIR__ constant to get the path to these files.
If header.php and footer.php are in the same directory as index.php, use this code:
include_once(__DIR__ . "/header.php");
include_once(__DIR__ . "/footer.php");
If they are in a subdirectory, e.g. includes then use the code:
include_once(__DIR__ . "/includes/header.php");
include_once(__DIR__ . "/includes/footer.php");
However, if they were in another place, you use such code:
include_once(__DIR__ . "/../../includes/another.php");
The __DIR__ contains the name of the current directory where the php file is located in which the constant is used.

Warning: include(dbconn.php): failed to open stream: No such file or directory

I have a site that I am have several issues with. I am not sure if the hosting company did changed something or the site was hacked. I hope this is OK as I have listed a list of errors when a user tries to register for the site. This is also happening when a user tries to use the search function, however in the search function the errors appear at the top and then the search results actually display below. The registration page does not show anything but the following errors.
Warning: include(dbconn.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 26
Warning: include(dbconn.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 26
Warning: include(): Failed opening 'dbconn.php' for inclusion (include_path='.:/opt/php54/lib/php') in /home1/hoapres/public_html/users/register/index.php on line 26
Warning: include(functions.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 27
Warning: include(functions.php): failed to open stream: No such file or directory in /home1/hoapres/public_html/users/register/index.php on line 27
Warning: include(): Failed opening 'functions.php' for inclusion (include_path='.:/opt/php54/lib/php') in /home1/hoapres/public_html/users/register/index.php on line 27
Fatal error: Call to undefined function userText2Web() in /home1/hoapres/public_html/users/register/index.php on line 40
Without knowing your directory structure it is tricky to offer a precise answer, but the first thing I would do before trying to include files is to explicitly set your include path.
Assuming that the site root ( DOCUMENT_ROOT ) is /home1/hoapres/public_html and you have a folder for storing all your include files called includes, the path would be /home1/hoapres/public_html/includes
<?php
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/includes' );
if( !realpath( get_include_path() ) ) exit('Bad path');
/* Test to see what files exist perhaps */
print_r( glob( get_include_path() . '/*.*' ) );
/* Can you see the files you expect? */
include 'dbconn.php';
include 'functions.php';
?>
Your problem is include path you have used is not correct.
Please check your code where you have include the files like
include("dbconn.php");
you can try by this way
$fullpath = "http://yourdomain.com/";
if the dbconn.php is under any folder then
$fullpath = "http://yourdomain.com/foldername/";
include($fullpath."dbconn.php");

failed to open stream in php

I get this error on a simple include method:
Warning: include(/home/content/70/8352870/html/sites/mysite.com/preview/wp-content/themes/site-theme/mail/class.phpmailer.php)
[<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in /home/content/70/8352870/html/sites/mysite.com/preview/wp-content/themes/site-theme/mail/send-message.php on line 5
I've included the file using this:
<?php
$path = getcwd();
include $path.'/class.phpmailer.php';
?>
I get the same error without using getcwd method but this:
include 'class.phpmailer.php';
Warning: include(class.phpmailer.php)
[<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in /home/content/70/8352870/html/sites/mysite.com/preview/wp-content/themes/site-theme/mail/send-message.php on line 10
Obiouvsly I checked the files are located here:
mysite.com/preview/wp-content/themes/site-theme/mail/
class.phpmailer.php
class.pop3.php
class.smtp.php
send-message.php
What did I do wrong?
This has the same behavior that you are trying to get, but is more trusted:
(I suppose that class.phpmailer is on the same path the the script that is including it)
<?php
$path = dirname( __FILE__ );
include $path.'/class.phpmailer.php';
?>

Why is this not working in WordPress functions.php?

$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.

How can I set an absolute path for include function in php above the working directory?

I am running a script from
/wp-content/themes/currenttheme/chat.php
I want to include in the above php another one located in
/forum/chat/index.php
The index.php includes its own files
I already tried
$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/forum/chat/index.php");
but I get this error
Warning: require(D:/My Dropbox/xampp/htdocs/lib/custom.php) [function.require]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17
Fatal error: require() [function.require]: Failed opening required 'D:/My Dropbox/xampp/htdocs/lib/custom.php' (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17
(the index.php also includes some files, but the /forum/chat is ommited somehow in the path)
then I tried
$path = getcwd();
$myfile = "/forum/chat/index.php";
include ($path.$myfile);
and got this error:
Warning: include(D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php) [function.include]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24
Warning: include() [function.include]: Failed opening 'D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php' for inclusion (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24
There is no problem with index.php. It is being included.
The error message says about custom.php file
Just use the same $_SERVER['DOCUMENT_ROOT'] technique for the custom.php
you have to add /forum/chat manually as there is no path to be omitted
Something wrong with:
include('../../../forum/chat/index.php');
?
There are all sorts of reasons why the code you've published will fail.
C.
use this.
require_once(ABSPATH.'forum/chat/index.php');
here ABSPATH = WordPress physical root directory path with trailing slash

Categories