Here is directory structure
/global.php
/includes/class_bootstrap.php
/includes/init.php
/plugins/myplugin.php
Here is codes in these files
/start.php
require('./includes/class_bootstrap.php');
/includes/class_bootstrap.php
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once(CWD . '/includes/init.php');
/plugins/myplugin.php
require_once(dirname(__FILE__).'../global.php');
And as far as I am understanding the problem is in class_bootstrap.php file coz it's generating wrong path for CWD
here is error:
Warning: require_once(C:/wamp/www/vb4/plugins/includes/init.php) [function.require-once]:
failed to open stream: No such file or directory in C:/wamp/www/vb4/global.php on line 35
As you can see "C:/wamp/www/vb4/plugins/includes/init.php" is wrong path.
The MAIN PROBLEM is that I can edit only myplugin.php file other files are CMS core files and should not be changed.
How can I fix this issue?
If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.
Instead, base it on a known path.
/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:
define('CWD', realpath(dirname(__FILE__) . '/../') );
dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins
Phew.
From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php
Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.
Related
In my php project I try to include another file. However, I find it very confusing how the include statement works.
e.g.
I include the file HelperFile.php in index.php. Both files are in the same directory.
This works: include 'HelperFile.php' but this doesn't include '/HelpferFile.php' & include './HelpferFile.php'
The warning I receive:
PHP Warning include(/HelperFile.php): failed to open stream
Out of curiosity I created a folder and moved my file HelperFile.php into it and nothing changed. Everytime I tried to use the relative path with ./, ../ or /I received a warning.
Can someone explain me what's going on. I'm still learning and can'f figure out what's happening right now.
PHP isn't so great with relative paths, it generally prefers absolute paths. The easiest way around this is to use the DIR magic constant which returns the current directory of the file you're currently in.
So, for instance, you can do include(__DIR__ . '/HelperFile.php'); which would be in the current directory.
However say you had a file in a folder up you can do
include(__DIR__ . '/../MyOtherFile.php');
PHP Doc says
If a path is defined — whether absolute (starting with a drive letter
or \ on Windows, or / on Unix/Linux systems) or relative to the
current directory (starting with . or ..) — the include_path will be
ignored altogether. For example, if a filename begins with ../, the
parser will look in the parent directory to find the requested file.
If you use . or .. will ignored for relative path Also use ../ for parent directory.
this is kind of a silly question, but as I can't sort it out I thought it might be good to get some help. The point is that the ".. /" to go back directory is not working.
The file I'm executing is in a folder that's on the main route and I need to go back to the main route and then enter another folder to load this other PHP file but it's not working what could be causing this issue.
ERRORS:
Warning: require_once(../PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in things/public_html/classes/Mail.php on line 3
Fatal error: require_once(): Failed opening required '../PHPMailer/PHPMailerAutoload.php' (include_path='.:/opt/alt/php71/usr/share/pear') in things/public_html/classes/Mail.php on line 3
DIRECTORY STRUCTURE:
File where the requiere once is:
/public_html/classes/filethatwantstoacces.php
File where it wants to get:
/public_html/PHPMailer/PHPMailerAutoload.php
require_once('../PHPMailer/PHPMailerAutoload.php');
What you should be using is the $_SERVER['DOCUMENT_ROOT'] variable. Please read this answer to another question for details.
If you are using PHP you should get into a habit of NOT using relative file paths at all but to use absolute paths, which will guarentee to succeed every time (As long as the target file exists and is reachable, etc.).
so; use $_SERVER['DOCUMENT_ROOT']
As a side note, you do not need to use brackets for your includes/requires, it's simply giving the server more work to do for no extra benefit.
The $_SERVER['DOCUMENT_ROOT'] is the base directory of your PHP/web application, typically the contents of the folder /public_html.
Using correct syntax and the above $_SERVER value (which will point to the /public_html folder you will have:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';
This will work from any script within your directory structure, if the file (PHPMailerAutoload.php) exists and is reachable at that given location
Given your location
/public_html/classes/filethatwantstoacces.php
doing ../ gives you
/public_html/classes
so ../PHPMailer/PHPMailerAutoload.php evaluates to
/public_html/classes/PHPMailer/PHPMailerAutoload.php
As #Martin has pointed out, using $_SERVER['DOCUMENT_ROOT'] to construct an absolute path to your file is the easiest way to avoid relative directory navigation errors such as this:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';
For requiring files in my PHP scripts I am using the following code :
require(dirname(__FILE__)."/../config.php");
with the config.php file being located a level higher than the file requiring it. However this appears to not work and the file cannot be located, with the following error :
failed to open stream: No such file or directory in /home3/**myusername**/public_html/PHP/access/login.php on line 3
I'm not sure what the error is as I have looked online and this appears to be the way others have done it. I believe however that the /.. is not causing it to go up a level.
EDIT 1
I changed the code to
require_once($_SERVER['DOCUMENT_ROOT'] . "/PHP/config.php");
and still receive the same error.
EDIT 2
The reason I am using an absolute path is because I have been led to believe this will work no matter where I call the file from, i.e if a file in a different directory includes this file (for this particular file it wont be the case but it will be in other files where I will use this) it will still include the config.php file correctly and not relative to the path of the file that included login.php.
EDIT 3
if I vardump the require path it prints the following :
string(52) "/home3/*myusername*/public_html/PHP/access/../config.php"
so obviously not going to the right location.
EDIT 4
Absolute path of file being required
/home3/*myusername*/public_html/PHP/config.php
Absolute path of file requiring it
/home3/*myusername*/public_html/PHP/access/login.php
try providing relative path to root. This is a better approach then providing relative to current file.
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
Remove the first / - "../config.php"
I'm looking to switch over to a new host, and they provide this nice little "temporary url" to test out your files before you switch. All fine and dandy. So I copy over all of my files. At the top of every page I require another file from the server that is stored at public_html/includes/head.php. Now for whatever reason, the $_SERVER['document_root'] var is returning /public_html/htdocs/includes/head.php (which does not exist on the server) and not /public_html/includes/head.php (which does exist). The resulting error is as follows:
Warning: require_once(/home/secure31/public_html/htdocs/includes/head.php)
[function.require-once]: failed to open stream: Permission denied in
/home/stephe80/public_html/index.php on line 6
The guilty code:
require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/head.php');
I am inclined to think that this is an error associated with their temporary url setup, but I do not want to transfer over my DNS and find that all of my files are broken. I could change them to absolute paths, but I am curious as to whether I am missing something. Any insight would be appreciated. Thanks!
I'd suggest to use the __FILE__ constant and work out your htdocs folder from there. It's much more reliable than relying on the $_SERVER superglobal, since that actually differs from server to server.
Example (that you'd call in your index.php in root folder):
$htdocs = str_replace(basename(__FILE__), __FILE__, '');
define('ROOT_FOLDER', $htdocs);
require_once(ROOT_FOLDER . '/includes/head.php');
For future reference (if anyone has similar issues), it turns out that the problem was with their test server. I launched it with my code as-was (risky, I know), and all turned out well.
Wondering why php keeps telling me a file doesnt exist when it does.
this is my code and error
require_once('/book/admin/bin/class/db.class.php');
Error and stack trace
Warning: require_once(/book/admin/bin/class/db.class.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\book\forms\add.php on line 3
Call Stack
# Time Memory Function Location
1 0.1479 408440 {main}( ) ..\add.php:0
This happens a lot and i want to rectify this annoying reoccurring problem. Is there a way I can set up my web server to read from the root of the site like I'm asking it too? Or am I misunderstanding what is happening.
This is how directory structure looks.
using a wamp server
c:\wamp\www\book <- my site root
c:\wamp\www\book\forms <- where add.php is located
c:\wamp\www\book\admin\bin\class\db.class.php
Why can I not use filepath as "/book/bin/"
Thanks,
C
Absolute paths on Windows starts with a drive letter. You can use
require_once 'c:\wamp\www\book\admin\bin\class\db.class.php';
or you can use a relative path.
To see what path you "start out" at use getcwd(); This is probably the directory where the script "starts", e.g. the directory where index.php is located.
echo getcwd();
You can require files relative to this dir.
However, I suggest you set a define a constant called APPLICATION_DIR or something like that and build links from that.
define('APPLICATION_DIR', 'c:\wamp\www\book');
require_once APPLICATION_DIR.'\admin\bin\class\db.class.php';