PHP include not working? - php

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

Related

Include a file from the root

Here is the structure of my application :
core
application
controller_base.class.php
registry.class.php
router.class.php
template.class.php
controller
include
config.php
model
views
index.php
The file config.php is included in the index.php file (there's no problem with this) and here is what it contains :
<?php
/*** include the controller class ***/
include(ROOT . '/core/application/controller_base.class.php');
/*** include the registry class ***/
include(ROOT . '/core/application/registry.class.php');
/*** include the router class ***/
include(ROOT . '/core/application/router.class.php');
/*** include the template class ***/
include(ROOT . '/core/application/template.class.php');
/*** autoload model classes ***/
function __autoload($class_name) {
$filename = strtolower($class_name) . '.class.php';
$file = ROOT . '/core/model/' . $filename;
if (!file_exists($file)) {
return false;
}
// include the $class_name class
include($file);
}
$registry = new registry;
You have to know that the constant ROOT is defined like this :
define('ROOT', str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
This constant contains the value /Lab/Livre/POO/TP1 which is of course the root of the applications.
But there's a problem whith all the files I include in config.php. PHP throws me an error that says the files are not found in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php.
So the problem is clear. I want PHP to look for the files from the root of the application (where index.php is), but it looks for it from the folder where config.php is located.
I had this problem for a long time but I want to find a solution once and for all.
I can't get it to do want I want ! I hope someone will help me.
Thank you very much.
P.S.: In other words, i want to include the files using an absolute path.
P.S.: Here is the full error text :
Warning: include(/Lab/Livre/POO/TP1/core/application/controller_base.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/controller_base.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 4
Warning: include(/Lab/Livre/POO/TP1/core/application/registry.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/registry.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 7
Warning: include(/Lab/Livre/POO/TP1/core/application/router.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/router.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 10
Warning: include(/Lab/Livre/POO/TP1/core/application/template.class.php): failed to open stream: No such file or directory in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
Warning: include(): Failed opening '/Lab/Livre/POO/TP1/core/application/template.class.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /Users/tom/www/Lab/Livre/POO/TP1/core/include/config.php on line 13
I have found a solution to my problem. I set the ROOT constant to this :
define('ROOT', $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']));
But what went wrong ?

php set_include_path drives me crazy

I need to include a file in php so I do
$web_proc = '../res/proc'; //variable read from config file I can't change
//in my file
include_once($web_proc . '/check_sprache.php');
and PHP outputs:
Warning: include_once(../res/proc/check_sprache.php): failed to open
stream: No such file or directory in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
Warning:
include_once(): Failed opening '../res/proc/check_sprache.php' for
inclusion (include_path='.:') in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
So I change the include path:
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__DIR__) . DIRECTORY_SEPARATOR);
and try it again, but PHP outputs:
Warning: include_once(../res/proc/check_sprache.php): failed to open
stream: No such file or directory in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
Warning:
include_once(): Failed opening '../res/proc/check_sprache.php' for
inclusion (include_path='.::/Users/user/Sites/site/res/') in
/Users/user/Sites/site/res/pdf/rechnung.php on line 62
But if if I do
include_once(dirname(__DIR__). DIRECTORY_SEPARATOR . $pfadweb_proc . '/check_sprache.php');
it works. But this is no solution, since the included file includes more files with a relative path, so they are alse not found.
So either I misunderstand the PHP include path, or it's just trolling me.
Can anybody help?
The include path looks odd in the second example; You have two PATH_SEPARATOR characters and a trailing DIRECTORY_SEPARATOR though I doubt this is the problem.
Try this one out instead
set_include_path(implode(PATH_SEPARATOR, [ // use array() if PHP < 5.4
dirname(__DIR__), // this should be /Users/user/Sites/site/res
get_include_path()
]));
include_once 'proc/check_sprache.php';
This will add the parent directory (/Users/user/Sites/site/res) of the CWD (/Users/user/Sites/site/res/pdf) to your include path.
Any include statements in any other files can use a relative path from /Users/user/Sites/site/res.

include in php not working

<?php
include("/Crypt/RSA.php");
$rsa = new Crypt_RSA();
$token=base64_decode("iKmHdK3ChRBPAU/I/wTKld4up91TMrcWjkE+VGggVryRzvhKC6l+sZ3F+j+qyW8rxg01/uu2E3z6aVirwmQ0ig==");
$private=base64_decode("MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAyUFH4OJOUOKh38raMxiQhmtuNSMxcznSdt9fWiJZOYpnv1rbu3h/heNCIOxOHSrlMz8FAKAW6rd9ddXNcm4myQIDAQABAkEApGbPcMVtdGWuFkJ/PH40kZnwzTeSja4OX0zZd6fXe0hBZZjA1nREuLGh2x7OXkpArytgQ35W2NHCxeldniTmgQIhAO7SB0Mhb/HLst4ty6HT2kZoAC/N2UdsBtQFdC8sNNxXAiEA17t4cVsx5EYYFifDSUwawz5pJSfrQYk1C0H1atL9Id8CIQDOtJL8k7BkxD5o95JM2yUN02518eGiY+n1EVNikQyfuQIgdph88fQsTU2rWCKr3NOVstfQfbigP/rpyjKMdBlhRwkCIE/x13OF2JUHlA5DqxCVh3LHMDDowr7SkQ2QVkqfBcAb");
$rsa->loadKey($privateKey);
echo $rsa->decrypt($cipher);
?>
i kept my include_path as .:/usr/local/lib/php
i am getting errors like this:
Warning: include(/Crypt/RSA.php) [function.include]: failed to open stream: No such file or directory in /home/futureti/public_html/reg2.php on line 2
Warning: include() [function.include]: Failed opening '/Crypt/RSA.php' for inclusion (include_path='.:/usr/local/lib/php') in /home/futureti/public_html/reg2.php on line 2
Fatal error: Class 'Crypt_RSA' not found in /home/futureti/public_html/reg2.php on line 3
Try this:
include('../Crypt/RSA.php');
Your current leading / is creating an absolute link, so going right back to your root directory.
../ means one folder up.
Try using the document root variable:
$doc_root = $_SERVER['DOCUMENT_ROOT'];
include($doc_root . "/Crypt/RSA.php");

PHP warning from root directory

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';

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

Categories