Why is Require_Once not finding a file that exists on disk - php

I have the following code
require_once "../pages/appconfig.php";
require_once "../freezer/data_functions.php";
Starting yesterday I'm getting the following error on the second file
failed to open stream: No such file or directory in
for both of these files. It has been working up until today.
echo realpath("../freezer/data_functions.php");
I get the correct full file path of the file that exists on disk.
I made a bunch of changes to other files but none that should affect these calls.
What could be causing this?

It turned out that as part of the appconfig.php includes I was calling a function that called
chdir(__DIR__ . "\\..\\");
and even though I changed it back again, I still got these odd issues. Removed the call to chdir and problem solved.

Related

Fail with nexted includes

I have a script that includes a file and that file has an include in it, like this:
In script:
include('includes/functions/homepage.php);
In homepage.php:
include('includes/functions/parent_functions.php');
I searched here and see it is a very common problem and the solution seems to be to use
include(dirname(__FILE__) . '/includes/functions/homepage.php');
I have the above and still get this error:
Warning: include(includes/functions/parent_functions.php): failed to open stream: No such file or directory
I've tried this on a site running php 5.5 and another using 7.2 - fails the same on both. If I print the path using the following it shows the correct full path.
echo dirname(__FILE__) . '/includes/functions/homepage.php';
As mentioned, this is a common question here but the fix isn't working in my case. Can anyone see why?
Well, you have an issue here. As the parent_functions.php and homepage.php are in the same directory you need not put an extra directory prefix. You can simply use
include('parent_functions.php');
instead of
include('includes/functions/parent_functions.php');

Specific project PHP files returning blank pages

So, I have this project uploaded at webwire.in/alpha , but it's returning blank pages.
PHP Fatal error: require(): Failed opening required './classes/Cookie.php' (include_path='.:/usr/share/php:/usr/share/pear in /var/www/html/alpha/core/init.php on line 22
and one on line 25.
Line 21-23 code = spl_autoload_register(function($class) {
require_once ('./classes/' . $class . '.php'); //line 22
});
Line 25 = require_once ('./functions/sanitize.php');
Tricky thing about this error is that this works only for this alpha folder. It was occurring yesterday also but somehow got fixed. Now when I deleted the files and uploaded, the error is back. Yesterday, at least the alpha/admin was working, but today nothing is working in this alpha folder.
One more thing, I've uploaded the same file to a different server, and it works flawlessly. See here - crowdsourced.in/rationshop (shared hosting)
Current server is a VPS if that matters.
I am not expert here, still tried solutions as I searched, but none worked for me till now.
Any help is highly appreciated.
Pages included are included relative to the page that was loaded, not the script doing the include.
So, if you have a script in the folder: /var/www/html/alpha/admin/
And that includes the script: /var/www/html/alpha/core/init.php
And init.php includes the file ./functions/sanitize.php
Then init.php is looking relative to the folder /var/www/html/alpha/admin/ which is where the initial script was loaded. Not looking relative to the init.php script. It is looking for a file at: /var/www/html/alpha/admin/functions/sanitize.php which doesn't exist. I'm assuming it exists under /var/www/html/alpha/core/functions/sanitize.php.
The easy fix is to use __DIR__ which is a built in php magic constant that has the value of the current script (the currently included file). Inside of /var/www/html/alpha/core/init.php, __DIR__ == /var/www/html/alpha/core/. So you can include/require __DIR__."/functions/sanitize.php

PHP require_once error when including file in parent folder

I have the following file structure
-- Plugins
-- inParent.php
---- Uploadify
------ inSame.php
------ Uploadify.php
This function was working smoothly till yesterday. But now, If I try to include the inParent.php in uploadify.php using require_once('../inparent.php), It giving me a silly error..saying...
Warning: require_once(../inParent.php) [function.require-once]: failed to open stream: No such file or directory in /home/user/public_html/plugins/uploadify/uploadify.php on line 3
But the file definitely exists...
The same code when used to include inSame.php shows no error and works perfectly..
I guessed that there could be file permission problem.. But everything is fine..all the related files have 755/644 permissions.
Also, the move_uploaded_file function has also stopped working.
All the code was working fine till yesterday. I hav'nt changed anything.
I have tried everything and dont know what exactly happened..
Please help me friends.. :( :(
I've googled and found this :
require_once __DIR__.'/../inParent.php';
In my case I had to write:
require_once dirname( __FILE__ ) . '/' . '../../inParent.php';
And that worked!
This question is old, but for people like me who run across it, here is an answer from another part of Stack Overflow.
require_once($_SERVER['DOCUMENT_ROOT'] . '/inParent.php');
PHP require file from top directory
If it was working yesterday and you are certain you did not change anything, then try a server restart first.
Second, on require once try using the full path instead of .. I normally create a file with my root paths and referance those instead of using ./ and ../
In my case it is really weird:
This Works perfect:
require('../../the_folder/the_file.php');
But PHP gives an error if i write that as this:
require('../the_file.php');
Please see that in case 2 i do not go up two steps (../../) and then down to the folder, i directly go up one step (../).
Why is PHP giving the error Failed to open stream: No such file or directory in ... ? It is really weird.
This also works:
require(__DIR__.'/../the_file.php');
Thanks for the TIP.

Web application not finding file in what appears to be the correct path

I tried to reorganise my facebook application files into better folders and now when I try to load my application on facebook it throws the following error:
Warning: require_once(../scripts/mysql_connect.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
Fatal error: require_once() [function.require]: Failed opening required '../scripts/mysql_connect.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
The second error is a result of the failure of the first one I think. The weird thing is that my program works perfectly fine when I'm accessing through the localhost url in the browser but when I run it through facebook it throws this error.
In my root folder I have a "Model" folder where the Database.php file is kept and a "scripts" folder where the mysql_connect.php file is kept. I have tried clearing my cache on the browser and using private browsing but it is still throwing this error.
Does anyone know what could be wrong?
EDIT: Sorry this does not seem to be working in localhost either now! One thing I think is weird is that it says "failed to open stream : NO SUCH FILE OR DIRECTORY IN" and then lists the database.php path. Why is it looking in there?
I use the line require_once('../scripts/mysql_connect.php'); at the top of my Database.php file to include it.
It seems that on the server there is a different working directory. Something like the following owuld be a quick fix
dirname(__FILE__) . '/../scripts/mysql_connect.php'
Ok I've fixed this, but I'm still unsure why it was a problem. In my root folder I have two folders: scripts, and Model. I have a database.php file in my Model folder which has the following line of code in:
require_once('../scripts/mysql_connect.php');
For some reason this did not work, but by replacing the line with:
require_once('/scripts/mysql_connect.php');
This started working again! Just thought I would post the solution. Does anyone know why the other way failed? I thought the ".." just makes it go up one level in the folder hierarchy i.e. to the root folder in this case, and then follow the path which would have taken it to the file...
EDIT: This appears to only have solved it for localhost and not through Facebook...
Do you have permission to edit your php.ini file? One solution would be to find the line with the include path (you'll notice it has C:\xampp\php\PEAR in there already). Include the full path to the "scripts" folder you're referencing, then you can change the php code to read:
require_once('mysql_connect.php');
PHP will be able to find this file since it's in the include path. Just make sure that if you start adding several include paths that you shouldn't have a file named mysql_connect.php in two or more of the different include paths because you'll most likely get errors. Hope this helps. It definitely makes for some cleaner code.

PHP Require Path

I have a directory layout something like /public_html/foo/bar.php that I want required in my file with my file require statement being currently require('../foo/bar.php') but I'm not getting to it (I keep getting an error saying its not a directory? and failed to open stream).
My file that I'm putting the require in is in /public_html/foo2/ so I need to go up one directory listing and then find the file to include it but I'm not sure what I'm doing wrong right now. Any guesses?
Actual code:
require('../objects/user.php/');
Thanks for your help!
You have a / behind your ../objects/user.php suggesting user.php is a directory.
That's why it tells you it is not a directory, because it indeed isn't.
Remove it and you'll probably be fine. (so ../objects/user.php/ becomes ../objects/user.php)

Categories