failed to open stream using include - php

Hello all this is a simple question which is already there in stack overflow but I tried all the possible answers still it's not fixed
I need to include config file which is under assets folder i tried the following code
include_once '../config.php' ;
am getting error like this
Warning: include(assets/config.php): failed to open stream: No such file or directory in E:\xampp\htdocs\project\assets\handler\ContactHandler.php on line 4
Warning: include(): Failed opening 'assets/config.php' for inclusion (include_path='E:\xampp\php\PEAR') in E:\xampp\htdocs\project\assets\handler\ContactHandler.php on line 4
kindly suggest me a possible way to include config file in ContactHandler.php
ContachHandler.php
<?php
include_once 'E:\xampp\htdocs\project\assets\config.php';
echo "ddddd";
exit;
include (CONTROLLER.'ContactController.php');
$form = $_POST['form'];
$cnt_controller = new ContactController();
switch ($form)
{
case 'AddContact':
$cnt_controller->AddContact($_POST);
break;
case 'ContactUs':
$cnt_controller->ContactUs($_POST);
break;
}
?>

Check to see if your include path changed and check file/folder permissions.
Try out this code:
<?php
$config_path = '../config.php' ;
if (file_exists($config_path)){
require_once $config_path;
}
else {
$config_path = 'config.php';
if (file_exists($config_path)) {
require_once $config_path;
}
else {
$config_path = '../../config.php';
require_once $config_path;
}
}
echo "ddddd";
echo $config_path;
exit;
?>
For testing set static path:
include_once 'E:\xampp\htdocs\project\assets\config.php';
Hope this helps!

Related

How can I output Math.php in PHP?

Warning: include(Math.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\php ModulesAndNamespaces\php modules & namespaces.php on line 13
<?php
// modules-> divide and reuse codes
//Math.php
define( 'PI', 3.14);
function add($a,$b) {
echo $a+$b;
}
?>
<?php
//App.php
include ('Math.php');
echo PI;// 3.14
echo add(1,2);//3
?>
Why do I get this error, and how can the problem be fixed?

Why can't php find this file?

I have an Apache/PHP site running on a Drobo5n in Linux.
utilities.php is in /Choir/inc
hitCounter.txt is in /Choir/etc/tbc
In utilities.php, we have the following line of code:
$hits = file_get_contents('../etc/tbc/hitCounter.txt');
Which produces this error:
Warning: file_get_contents(../etc/tbc/hitCounter.txt): failed to open
stream: No such file or directory in
/mnt/DroboFS/Shares/DroboApps/apache/www/Choir/inc/utilities.php
on line 6
This is my first time fiddling with PHP and I cannot figure out why it can't find the file. I've tried both single and double quotes around the path to no avail.
I know someones gonna ask for the complete code so here's the utilities.php file:
<?php
session_cache_limiter('private_no_expire');
session_start();
function getHitCount() {
$hits = file_get_contents('../etc/tbc/hitCounter.txt');
if (!isset ($_SESSION['beenHere'])) {
$hits = $hits + 1;
file_put_contents('../etc/tbc/hitCounter.txt', "$hits");
$_SESSION['beenHere'] = "Yes I have";
}
return $hits;
}
?>
1) Should explicit your file path. Hard to say in this case. We should have our root application folder.
If we follow the MVC pattern, we will get the root application folder easily.
For example https://github.com/daveh/php-mvc
I like something:
$file = APP_ROOT . '/etc/tbc/hitCounter.txt';
#APP_ROOT has the path /mnt/DroboFS/Shares/DroboApps/apache/www/Choir
2) Check file_exists
if (!file_exists($file)) {
//Throw error here
}
3) Check: is_readable
if (!is_readable($File)) {
......
}

Require_once failed to open stream: No such file or directory

I have this error:
Warning: require_once(../questionTypes/Question.php): failed to open stream: No such file or directory in /home/u949913003/public_html/includes/essential.php on line 25
Fatal error: require_once(): Failed opening required '../questionTypes/Question.php' (include_path='.:/opt/php-5.6/pear') in /home/u949913003/public_html/includes/essential.php on line 25
I require essential.php from New_test.php like this:
require_once('../essential.php');
Then in essential.php, I require Question.php like this:
require_once($config['systemQuestionTypesClassDir'].'Question.php');
$config is defined in config.php:
$config['controller']['a'] = 'Admin';
$config['controller']['e'] = 'Teacher';
$config['controller']['t'] = 'Teacher';
$config['controller']['s'] = 'Student';
$config['controller']['at'] = 'Teacher';
$config['controller']['er'] = 'Teacher';
// System directories
$config['systemControllersDir'] = '../controllers/';
$config['systemQuestionTypesClassDir'] = '../questionTypes/';
$config['systemViewsDir'] = '../views/';
$config['systemLibsDir'] = 'libs/';
$config['systemLangsDir'] = 'langs/';
$config['systemQuestionTypesLibDir'] = $config['systemLibsDir'].question;
$config['systemLangsXml'] = '../resources/languages/';
$config['systemExtraDir'] = = 'extra/';
$config['systemFpdfDir'] = 'fpdf/';
$config['systemPhpGraphLibDir'] = 'phpgraphlib—master/';
$config['systemFileManagerDir'] = 'filemanager/';
My folders structure is this:
Obviously in questionTypes there is Question.php.
I don't know ho to solve this.
Calling "include" from another "include" could cause problems when they are relative paths. If you use the absolute path, it shouldn't be a problem.
You could use __DIR__ to get the directory that contains the file where this is written.
I think this should works:
File: New_test.php
require_once(__DIR__ .'/../essential.php');
File: essential.php
require_once(__DIR__ .'/../questionTypes/Question.php');

cannot move uploaded files failed to open stream

Hello so i have a simple upload system in php and i want to upload my files to ftp server but when i try to it doesnt work i get these two errors:
Warning: move_uploaded_file(/userfiles/grega): failed to open stream: No such file or directory in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
Warning: move_uploaded_file(): Unable to move '/tmp/phpVtApVM' to '/userfiles/grega' in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
and there is folder userfiles/grega on the ftp server please help me out
the code:
<?php
require_once 'core/init.php';
if($_POST[submit]) {
$name = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
if($size <= 5000000){
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
$uploads_dir = '/userfiles';
$username = ($user->data()->username);
move_uploaded_file($temp,"$uploads_dir/$username");
Session::flash('home', '<h3>Datoteka je bila naložena!</h3>');
Redirect::to('mojprofil.php');
} else{
echo "Napaka!";
}
} else {
header("Location: mojprofil.php");
}
?>
You say this:
there is folder userfiles/grega
But the error says this:
move_uploaded_file(/userfiles/grega)
Those are two very (even if subtly) different paths. Note also where you define the path in your code:
$uploads_dir = '/userfiles';
The code is looking for a folder called userfiles in the root of the entire file system, not just in the website. Perhaps you meant to do this?:
$uploads_dir = 'userfiles';

PHP Config.php for write to flat file

The php code grabs values from a form and writes it to flat file. This code works locally but when I place it on the host I get the the two errors. Local instance doesn't require a config.php in the directory but the host file does.
error1: states that there is no config.php
error2:I have to specify a path but if
? - what do I put in the config file so it allows me to create the flat file or can I reference the file same how??
any help would be appreciated.
CODE:
<html>
<head>
<title>test</title>
</head>
<body>
<?php
include('config.php');
$user = $_GET["name"];
$message = $_GET["message"];
print("<b>Thank You!</b><br />Your information has been added! You can see it by <a href=savedinfo.php>Clicking Here</a>");
$out = fopen( $_SERVER['DOCUMENT_ROOT'] . '/flatfile/test/' . $user . '.txt', "a" );
if (!$out) {
print("Could not append to file");
exit;
}
fputs ($out,implode,("\n"));
fwrite($out,"<b>$user</b><br />$message<br /><br />");
fclose($out);
?>
</body>
</html>
ERROR:
1
Warning: include(config.php) [function.include]: failed to open stream: No such file or directory in /home/user1/flatfile/sendinfo.php on line 7
2
Warning: include() [function.include]: Failed opening 'config.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/user1/flatfile/sendinfo.php on line 7
You have 2 ways: create config.php or remove include.
Are you sure the config.php file is in the right directory you can check if the file exist and then include like this
if (file_exists('config.php')) {
include('config.php');
}
http://php.net/manual/en/function.file-exists.php

Categories