PHP file does exist but can't require_once() - php

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

Related

require_once function error when creating webpage in new folder

I am running wamp64 to locally host my website (it is a vbulletin forums used for testing if that helps at all).
I am trying to create a new webpage that is located in a subfolder instead of the root of the website, however I am running into errors.
The webpage I am trying to create is the index.php webpage inside of the addons folder.
My file structure is:
C:
-wamp64
--www (website root)
--global.php
---addons
----index.php
----images
---includes
----config.php
The code I am using for addons - index.php is:
<?php
error_reporting(E_ALL & ~E_NOTICE);
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'addons');
$phrasegroups = array();
$specialtemplates = array();
$globaltemplates = array(
'ADDONSTESTPAGE'
);
$actiontemplates = array();
require_once('/../global.php');
eval('$navbar = "' . fetch_template('navbar') . '";');
eval('print_output("' . fetch_template('ADDONSTESTPAGE') . '");');
?>
When I create a new page in the www folder, it works perfectly (except I use require_once('/global.php'); instead of require_once('/../global.php');.
However, because this is in a subfolder, I am running into the following error due to the files that the global.php is requiring once having their own require_once functions inside of them and those files have their own require_once functions inside of them, etc.
Here is the error I am getting now when I go to localhost/addons/:
Warning: require_once(C:\wamp64\www\addons/includes/init.php): failed
to open stream: No such file or directory in C:\wamp64\www\global.php
on line 20
Fatal error: require_once(): Failed opening required
'C:\wamp64\www\addons/includes/init.php'
(include_path='.;C:\php\pear') in C:\wamp64\www\global.php on line 20
Here are lines 13-20 of global.php:
// identify where we are
define('VB_AREA', 'Forum');
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
// #############################################################################
// Start initialisation
require_once(CWD . '/includes/init.php');
How do I solve this without messing up other files that use the global.php file? I have heard realpath is a common solution for this, but I am new to php and not exactly sure how to use it in this scenario.
All help is appreciated, thank you very much, if you need any more information feel free to ask!
Okay I was able to solve that error by changing require_once(CWD . '/includes/init.php'); to require_once(__DIR__ . '/includes/init.php');.
Now I am getting this error:
Warning: require_once(C:\wamp64\www\addons/includes/class_core.php): failed to open stream: No such file or directory in C:\wamp64\www\includes\init.php on line 51
Fatal error: require_once(): Failed opening required 'C:\wamp64\www\addons/includes/class_core.php' (include_path='.;C:\php\pear') in C:\wamp64\www\includes\init.php on line 51
Class_core.php exists in the includes folder.
Line 51 of init.php is:
require_once(CWD . '/includes/class_core.php');
I tried changing it to require_once(__DIR__ . '/includes/class_core.php'); but it breaks the entire website.
global.php assumes the server process is running from the main C:/wamp64/www directory but because you are running from the addon folder, CWD will be set to C:/wamp64/www/addon , hence all the errors.
To resolve this, do, in your index.php
1) Revert all the changes you have made to VBulletin in your global.php and init.php etc...
2) set your current working directory to the root directory, you can use chdir('../') at the top of index.php before any require_once
3) require global.php in your index.php require_once('global.php');
Step 2 works because now php now sees the root directory as the working directory.

PHP link not working

I'm trying to require_once a file in a document; here's my current syntax:
$path = $_SERVER['DOCUMENT_ROOT'];
require_once("includes/save-email.php");
The problem is that, when I reload my page in my browser, every element disappears and I get the following two messages:
Warning: require_once(/includes/connect.php) [function.require-once]: failed to open stream: No such file or directory in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '/includes/connect.php' (include_path='.:/usr/lib/php5') in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
I've tried every variation of the link I can think of, including ../link, ./link, /link, and link.
Strangely enough, when I include an element of the layout using the same overall syntax (replacing require_once with include), that element loads without any errors, even though it's in the same directory.
I'm not sure that it matters, but my server is run by 1&1.
require_once(/includes/connect.php) will look for the file in the root of your server not in the current folder
repalace
require_once(/includes/connect.php)
by
require_once(includes/connect.php)
in save-email.php on line 2
setting $path won't do anything. you will need to have the includes folder in the same folder as this php document.

Issues with using PHP Swift Mailer

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

PHP include path error

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.

Troubleshooting PHP Login connection

I have encountered a problem that I have not come accross yet when setting up a log in page using php.
The page has a error message that relates to line 1 ( require_once('../Connections/Login.php)
that states
[function.require-once]: failed to open stream: No such file or directory
Fatal Error: require_once() [function.require]: failed opening required ... (include_path='.:/usr/share/pear-php5')
I know it is probably really simple and stupid, but I'm really struggling to self-troubleshoot and would really value some help please?
This is where I have been so confused. I also thought the same, that it just couldn't find the file; however the structure is just as you have said:
htdocs/comparison.php (the log in page)
htdocs/Connections/connComparisonLogin.php
Sorry this is going to confuse you, I simplified the actual script in my original question and here is the actual error message:
Warning: require_once(../Connections/connComparisonLogin.php) [function.require-once]: failed to open stream: No such file or directory in /home/fhlinux135/s/suzukigenuine.com/user/htdocs/comparison.php on line 1
Fatal error: require_once() [function.require]: Failed opening required '../Connections/connComparisonLogin.php' (include_path='.:/usr/share/pear-php5') in /home/fhlinux135/s/suzukigenuine.com/user/htdocs/comparison.php on line 1
Have I done it all wrong?
I think your require statement should be:
require_once 'Connections/connComparisonLogin.php';
Where is your "Connections/Login.php" relative to the currrent php file.
php will look for the file relative to the current file, or, in one of the directories specified in the "include_path" php.ini setting.
The error just means php can't find the Login.php file. Assuming the page you are calling is index.php you should have a director structure like
-Dir1
|-index.php
-Connections
|-Login.php
Why are you going up a directory in your require statement? Remove the "../" from the beginning of your require_once path.

Categories