This question already has answers here:
PHP Fatal Error Failed opening required File
(7 answers)
Closed 3 years ago.
I am working on a PHP project,i am getting an error in require_once(or even in require)
Warning: require_once(C:/wamp/www/MyFiles/MyProject/includes/db_config.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\MyFiles\MyProject\includes\autoloads.php on line 9
Fatal error: require_once() [function.require]: Failed opening required 'C:/wamp/www/MyFiles/MyProject/includes/db_config.php' (include_path='.;C:\php5\pear') in C:\wamp\www\MyFiles\MyProject\includes\autoloads.php on line 9
this is how i am including the file
$root = $_SERVER['DOCUMENT_ROOT'];
//config..
require_once($root . '/MyFiles/MyProject/includes/db_config.php');
i have tried using
echo $root . '/MyFiles/MyProject/includes/db_config.php';
It is printing URL properly
ie C:\wamp\www\MyFiles\MyProject\includes\db_config.php
I am using WAMP server 5 autload.php and db_config.php are in same folder
Double check if the file exists on the server. If you can't for whatever reason, this will do it:
if(file_exists($myFile)){
echo"The file is there";
}//end of file exists
else{
echo"the file does NOT exist....fool";
}//end of else - the file is not there
That's the only problem I can think of - the file is not where it should be... Try the above and keep me updated.
Might as well be a problem with the directory separator being diffrent on linux and windows machines.
Maybe try using the constant DIRECTORY_SEPARATOR instead of hardcoded separators?
check the existence of file, if exists, check the permission of file.
Try this require construction
require 'file';
..or this root settings
$root = realpath($_SERVER['DOCUMENT_ROOT']);
update
use virtual host
Is the file readable?
Try this:
<?php
echo "Does file exist?". file_exists($file) ? 'true' : 'false';
echo "Is it readable?".is_readable($file) ? 'true' : 'false';
require_once $file;
You can also use path relative to the file you're in, instead of relying on the DOCUMENT_ROOT string:
<?php
$root = realpath(dirname(__FILE__));
Additionally, the directory separate in windows is by default \, not / (alltough / should resolve aswell, even for require). There's a predefined constant you could use, just to make sure it's compatible everywhere though:
require_once($root . DIRECTORY_SEPARATOR . 'MyFiles' . DIRECTORY_SEPARATOR . 'MyProject' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'db_config.php';
Also, if you are using APC cache, you can attempt to restart it, if there's a stale cache of the file. (E.G filemtime is incorrect)
Related
So recently I decided to move my php code that was at the top of every page, and exactly the same to it's own php file. First thing I did before trying a require_once() on it was to make sure php could read the file so I did
<?php
if(file_exists('Backend.php')) {
echo 'File does exist';
}
?>
And it echo'd out that the file exists perfectly fine. Now whenever i try:
<?php
require('Backend.php') or die('Could not load Backend.');
?>
I get the error:
'Warning: require(1): failed to open stream: No such file or directory in
C:\Users\Owner\Dropbox\PotateOS\index.php on line 2
Fatal error: require(): Failed opening required '1'(include_path='.;C:\xampp\php\PEAR;C:\Users\Owner\Dropbox\PotateOS') in C:\Users\Owner\Dropbox\PotateOS\index.php on line 2
Note I am using(XAMPP). Things I have tried:
Adding the path of the files to php.ini's include_path
Checking the filename to make sure it doesn't have any strange charectars
Using this as the file path:
<?php
require($_SERVER['DOCUMENT_ROOT'] . 'Backend.php') or die('Could not load');
?>
(Note both the page i'm trying to access backend from, and backend are in the root directory)
The problem is that require is not a function but a language construct; as a consequence it will run your code as:
require('Backend.php' or die('Could not load Backend.'));
The expression 'Backend.php' or die('Could not load Backend.') yields bool(true) and cast to a string it becomes string(1) "1" and that will be attempted to load.
You can simply reduce your code to this:
require 'Backend.php';
It will raise an error if the file could not be found anyway and will halt the script. As mentioned in the comments, it's important to realise that the document root doesn't have a trailing slash; thus, you must add it yourself:
require $_SERVER['DOCUMENT_ROOT'] . '/Backend.php';
Before require add:
echo getcwd();
and then check if Backend.php is in the same directory as result of this function
What I have done:
) Downloaded Swift-4.1.5.tar
) Extracted it
) Uploaded to my host in /public_html/domain/lib using FileZilla
) Made a new script using the code below.
) Opened it in the Browser and I got the following error below
So my question is, how am I getting the error that the file doesn't exist if It's 100% there? Thanks!
<?php
require_once '/public_html/domain/lib/swift_required.php';
?>
Warning: require_once(/public_html/domain/lib/swift_required.php) [function.require-once]: failed to open stream: No such file or directory in /home/myuser/public_html/domain/email.php on line 5
Fatal error: require_once() [function.require]: Failed opening required '/public_html/domain/lib/swift_required.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/myuser/public_html/domain/email.php on line 5
Here's a screenshot of my dir from FileZilla.
http://i.imgur.com/Zsy8y.jpg
This is wrong:
// this is absolute path, just like /home/user or /var/www
require_once '/public_html/domain/lib/swift_required.php';
Use instead:
// this is relative path to the current file
require_once 'public_html/domain/lib/swift_required.php'; //notice: no '/' in the beggining
OR:
// so use this one if you know the whole path to the file
require_once ABSOLUTE_PATH_TO . 'public_html/domain/lib/swift_required.php';
OR:
// or use this one if you don't know the whole path
// or if the path will change (dev machine and production machine)
require_once dirname(__FILE__) . RELATIVE_PATH_TO . 'public_html/domain/lib/swift_required.php';
I have a file header where i have created variables for all the paths i need, like this:
$GLOBAL_stylePath = "http://localhost/pspace/css/";
(if i shouldn't use http in the above, then how would it be? C://htdocs/xampp/pspace/css/ ???)
include_once "/classes/authorizationUtils.php";
$authorizationUtils = new AuthorizationUtils();
anyways, the includes are messing up everything and giving me errors such as:
Warning: include_once() [function.include-once]: http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\pspace\includes\header.php on line 12
how can i enable this configuration in my php.in. i have a variable allow_url_include=off, when i "on" it, no changes happen. and also this:
Warning: include_once() [function.include]: Failed opening 'http://localhost/pspace/classes/authorizationUtils.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\pspace\includes\header.php on line 12
Make sure that you editing the correct php.ini file. Usually there are two php.ini files associated with your installation. One is used for the command line interface php and the other for the server side php. Make sure that you make the changes to both.
Make sure that you restart your webserver after these changes are made.
This warning :
Warning: include_once() [function.include]: Failed opening 'http://localhost/pspace/classes/authorizationUtils.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\pspace\includes\header.php on line 12
it means that you try to include file which could not be found in include_path. So, what you need is to define your include_path.
If your directory listing is like this :
c:\xampp\htdocs\pspace\ // as your root
c:\xampp\htdocs\pspace\includes\
c:\xampp\htdocs\pspace\classes\
Try this :
<?php
$path1 = '.\includes\';
$path2 = '.\classes\';
set_include_path(get_include_path() . PATH_SEPARATOR . $path1 . PATH_SEPARATOR . $path2);
?>
Your also can change relative path $path1 = '.\includes\'; to absolute path $path1 = 'c:\xampp\htdocs\pspace\includes\';
Read this to understand more about set_include_path and about include function that always include files based on file path given. Hope this can help.
I have come across as problem that has me scratching my head since a couple of days now. I am using jQuery Uploadify to upload files to a web server. I am using a slightly amended uploadify.php script that handles the file upload:
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . "/file-repository/" . $_REQUEST['folder'] . '/';
$targetFilename = str_replace(" ","-",$_FILES['Filedata']['name']);
$targetFilename = preg_replace('/[^a-zA-Z0-9-_.]/', '', $targetFilename);
$targetFile = str_replace('//','/',$targetPath) . $targetFilename;
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
}
?>
This script was working fine until yesterday. Now it throws the following error:
Warning:
move_uploaded_file(/home/server/public_html/devhealth-safety/administration/group/image.jpg)
[function.move-uploaded-file]:
failed to open stream: No such file or directory in
/home/tdserver/public_html/sb3/lib/js/uploadify/uploadify.php
on line 46 Warning: move_uploaded_file()
[function.move-uploaded-file]:
Unable to move \'/tmp/phpYH8nfH\' to
\'/home/server/public_html/devhealth-safety/administration/group/image.jpg\'
in
/home/server/public_html/dev/lib/js/uploadify/uploadify.php
on line 46
For some reason the . "/file-repository/" . portion in uploadify.php is dropped, which means that the script tries to move the file to a bogus path on the server.
Now, as I said, the script has worked and I haven't modified it. I have fiddled with the folder structures, but the file path is correct, the file-repository folder has all read/write/execute permissions and belongs to the PHP owner and owner groups (which in this case is nobody:nobody). So I would not think that my tinkering with the folders has anything to do with this problem (but who knows?)
Oddly enough, if I replace . "/file-repository/" . with . "/filerepository/" . the script throws the error I would expect:
Warning:
move_uploaded_file(/home/server/public_html/dev/filerepository/health-safety/administration/group/image.jpg)
[function.move-uploaded-file]:
failed to open stream: No such file or directory in
/home/tdserver/public_html/sb3/lib/js/uploadify/uploadify.php
on line 46
So the question is... why does PHP drop the string portion that contains the hyphen? Any ideas?
in your regular expression , the hyphen should be placed at last !!
'/[^a-zA-Z0-9-_.]/' should be '/[^a-zA-Z0-9_.-]/'
I keep getting an error when I try to include simpletest in my include_path:
<?
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(basename(dirname(__FILE__)) . '/../../'));
include 'simpletest/autorun.php';
returns:
.:/usr/lib/php:/Users/kristiannissen/Documents/php
Warning: require_once(/Users/kristiannissen/Documents/php/simpletest/arguments.php): failed to open stream: No such file or directory in /Users/kristiannissen/Documents/php/simpletest/reporter.php on line 13
Fatal error: require_once(): Failed opening required '/Users/kristiannissen/Documents/php/simpletest/arguments.php' (include_path='.:/usr/lib/php:/Users/kristiannissen/Documents/php') in /Users/kristiannissen/Documents/php/simpletest/reporter.php on line 13
personally your view pages are the first executed, such as index.php and view pages should always be in the root of your html.
so within index.php you can do:
define("BASE_PATH",str_replace("\\","/",dirname(__FILE__)));
so now BASE_PATH would be equal to: /Users/kristiannissen/Documents/php/simpletest
So if you want to phpdirectory in your include path you should use dirname() to go UP a directory:
//Get an array of your include paths
$include_parts = explode(PATH_SEPARATOR,get_include_path());
//Extend the paths
$include_parts[] = dirname(dirname(BASE_PATH)); //this is ../../
//recompile the paths and set them
set_include_path(implode(PATH_SEPARATOR,$include_parts));
this is the safe way to accomplish what your trying to do.
There is an alpha2-release on sourceforge that has the arguments.php-require commented out. I guess it isn't needed anymore.