Php require once ошибка

ERROR 500 Require_once

make sure the path is right and file exists. Then make sure the permissions are correct. I assume the php/ dir is inside the dir you are requireing from?

4 Answers 4

What this looks like to me is that the file UserDatabaseManager.php is trying to include DataBaseManager.php but it is providing the wrong path. Your apache error indicates that it will first look in . i.e. the current directory then /usr/share/pear then /usr/share/php

Your DataBasemanger.php file is not in either of the last two. When it attempts to look in the current directory it is including your relative path. So when your UserDataBaseManager.php file (which is in php/Classes/User/ ) provides that path then apache ends up looking for your DataBaseManager.php file in php/Classes/User/php/classes/DataBase/DataBaseManager.php

There are several ways to approach this. You could get the document root and prepend it to every include like this

$root = $_SERVER['DOCUMENT_ROOT']; require_once($root . "/php/classes/DataBase/DataBaseManager.php"); 

Or if this app is in a controlled environment and you can edit the php.ini file you can define the include path to also look in /var/www/virtuales/blyxo.com/php/Classes/ and just include the file with

require_once("Database/DataBaseManager.php"); 

A better solution might be to define an __autoload function that will be called every time you try to instantiate a new object and have it use some logic to look for the file to include. This solution would take a bit more restructuring but you might consider it. Here is the doc on defining your own auto loader http://us2.php.net/manual/en/function.autoload.php

I know there are other ways to do this. Let me know what works for you!

Источник

PHP Fatal Error Failed opening required File

I am getting the following error from Apache [Sat Mar 19 23:10:50 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [function.require]: Failed opening required ‘/common/configs/config_templates.inc.php’ (include_path=’.:/usr/share/pear:/usr/share/php’) in /home/viapics1/public_html/common/configs/config.inc.php on line 158 I am definately not an expert of Apache but the file config.inc.php & config_templates.inc.php are there. I also tried navigating to a test.html page I placed in common/configs/ so I assume there is no rights issues going on. I also set the rights on config_templates.inc.php to give everyone read, write, and execute rights. Not sure what to do at this point, I checked to see if there was a /usr/share/php directory and I found there was not but when I did yum install php it said it had the latest. Ideas?

7 Answers 7

It’s not actually an Apache related question. Nor even a PHP related one. To understand this error you have to distinguish a path on the virtual server from a path in the filesystem.

require operator works with files. But a path like this

 /common/configs/config_templates.inc.php 

only exists on the virtual HTTP server, while there is no such path in the filesystem. The correct filesystem path would be

/home/viapics1/public_html/common/configs/config_templates.inc.php 

part is called the Document root and it connects the virtual world with the real one. Luckily, web-servers usually have the document root in a configuration variable that they share with PHP. So if you change your code to something like this

require_once $_SERVER['DOCUMENT_ROOT'].'/common/configs/config_templates.inc.php'; 

it will work from any file placed in any directory!

Update: eventually I wrote an article that explains the difference between relative and absolute paths, in the file system and on the web server, which explains the matter in detail, and contains some practical solutions. Like, such a handy variable doesn’t exist when you run your script from a command line. In this case a technique called «a single entry point» is to the rescue. You may refer to the article above for the details as well.

Читайте также:  Таблица

@Al Katawazi nope, in your PHP code. you are addressing a file in your PHP code. And you have to use RIGHT address

Sorry this wasn’t it. I updated the code to look like this:

@Al Katawazi I’ve edited second part (using __FILE__ ), try it now. If fail, post it’s error message.

[Sun Mar 20 01:53:43 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [function.require]: Failed opening required ‘/home/viapics1/public_html/photo/common/configs/config.inc.php’ (include_path=’.:/usr/share/pear:/usr/share/php’) in /home/viapics1/public_html/photo/index.php on line 2. That directory doesn’t exsist. Its trying to go off photo and then to common when it should go to the html root directory and then go into common. I think the code was right and there is some other potential issue going on, perhaps in actually opening the file.

If you have SELinux running, you might have to grant httpd permission to read from /home dir using:

 sudo setsebool httpd_read_user_content=1 

Run php -f /common/configs/config_templates.inc.php to verify the validity of the PHP syntax in the file.

The error message makes it clear that file doesn’t exist, so what’s the point of RERUNNING php just to confirm that it doesn’t exist? OP has a leading slash, turning the path into an absolute one. It should be at minimum a relative path with no leading slash.

«The error message makes it clear that file doesn’t exist.» No it doesn’t. That identical error message could result from include path settings, file permissions settings, or safe mode settings. (I was also guessing that it could come from syntax errors, depending on the error reporting settings, but after some testing, it looks like PHP always shows the actual syntax error.)

You are wrong. Each of cases you mentioned has its own distinct error message. As well as syntax error. One could easily distinguish «parse error» from «file not found error». Your assumptions are all wrong and misleading. Get more experience with PHP

My error_log includes the stack trace, which contains the same text for these different conditions. E.g., here’s a line pulled from my log just now (w/ paths obscured), for a non-existing file: PHP Fatal error: require(): Failed opening required ‘sub/include.php’ (include_path=’.:/usr/local/lib/php’) in /path/to/test.php on line 4» And here’s one for an existing file with bad perms: PHP Fatal error: require(): Failed opening required ‘sub/include.php’ (include_path=’.:/usr/local/lib/php’) in /path/to/test.php on line 4″» Ditto for a bad include_path.

Читайте также:  Хорошие скрипты для html

check file perms, dude. failed to open stream: Permission denied is an error message for the bad perms

You could fix it with the PHP constant __DIR__

require_once __DIR__ . '/common/configs/config_templates.inc.php'; 

It is the directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname __FILE__ . This directory name does not have a trailing slash unless it is the root directory. 1

Just in case this helps anybody else out there, I stumbled on an obscure case for this error triggering last night. Specifically, I was using the require_once method and specifying only a filename and no path, since the file being required was present in the same directory.

I started to get the ‘Failed opening required file’ error at one point. After tearing my hair out for a while, I finally noticed a PHP Warning message immediately above the fatal error output, indicating ‘failed to open stream: Permission denied’, but more importantly, informing me of the path to the file it was trying to open. I then twigged to the fact I had created a copy of the file (with ownership not accessible to Apache) elsewhere that happened to also be in the PHP ‘include’ search path, and ahead of the folder where I wanted it to be picked up. D’oh!

Источник

Fatal Error- Require_once

Warning: require_once(C:\xampp\htdocs\mvc\public\views\home\init.php): failed to open stream: No such file or directory in C:\xampp\htdocs\mvc\public\index.php on line 3 Fatal error: require_once(): Failed opening required ‘C:\xampp\htdocs\mvc\public\views\home\init.php’ (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\mvc\public\index.php on line 3

htdocs\mvc\app\views\home\init.php htdocs\mvc\app\core\app.php & controller.php htdocs\mvc\public\index.php

there has to be a problem with the file path. right now you’re pointing to a file in a views directory in the same place as index.php.. is this correct?

It seems that your index.php is located at public/ folder, while your required file views/home/init.php is located at app/ folder. So, try require ‘../app/views/home/init.php’; If it doesn’t work, you can try realpath (__FILE__.’../app’); to get your app/ folder’s realpath name.

4 Answers 4

First do echo for the file path in index.php file like —

echo __DIR__ . '/views/home/init.php'; 

And then check whether the path is correct or not. If not, then change it accordingly and then use the path in require_once .

this is what i get after putting echo. It seems path is correct: C:\xampp\htdocs\mvc\public/views/home/init.php Fatal error: Uncaught Error: Class ‘App’ not found in C:\xampp\htdocs\mvc\public\index.php:5 Stack trace: #0

thrown in C:\xampp\htdocs\mvc\public\index.php on line 5

How to check file permission? also should i use accept Try this: First do echo for the file path in index.php file like — echo DIR . ‘/views/home/init.php’; & require_once’views/home/init.php’; together in code or just after checking the path remove DIR and then use require_once

Читайте также:  Java debug running jar

echo __DIR__ . ‘/views/home/init.php’; this one is for testing only. Finally, you have to write your code like require_once __DIR__ . ‘/views/home/init.php’; and use DIR for other required lines.

after putting DIR in all three codes: Warning: require_once(C:\xampp\htdocs\mvc\publicviews/home/init.php): failed to open stream: No such file or directory in C:\xampp\htdocs\mvc\public\index.php on line 3 Fatal error: require_once(): Failed opening required ‘C:\xampp\htdocs\mvc\publicviews/home/init.php’ (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\mvc\public\index.php on line 3

Источник

require is working but require_once is not

When using require_once it is giving undefined variable route error while if i use only require it shows the route object. Why is that so ?

Something else must be causing the issue, as The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.

2 Answers 2

This kind of error only happens when the file is already loaded from other files. That’s why it’s not loading it again and you are not getting instance of $route object.

Let’s say file1.php included routes.php

  1. require ‘routes.php’ (it will load same file again even already loaded)
  2. requier_once ‘routes.php’ (it will not load the file if already loaded, and as you are not getting instance of $route variable, it’s mean it’s happening)

Point 2 says (it will not load the file if already loaded). If that is so, still $route instance should be available.

but if and only if the file is not loaded in a function or class as variable instance then can only get from there

Your simple answer is that use GLOBAL $route; and now you will always get that variable even if loaded in any scope. Hope you get it 🙂

All symptoms suggest that ‘app/routes.php’ is included somewhere else before. Since it defines a variable, if such include does not happen in global scope the variable will be local to wherever it’s called from.

Apart from using a dedicated debugger like Xdebug, you can use builtin tools to diagnose the issue. For instance, you have get_included_files() to get a list of included files in a given point. You can also add debug_print_backtrace() on top of ‘app/routes.php’ to find out where it’s called from.

Note on updated question and follow-up comment: if you’re trying to auto-load the file and the file gets loaded automatically, I’d say you’ve just answered your own question. But it’s worth noting that auto-loading is intended to be used on functions and class definitions. You have an arbitrary code snippet that defines a variable and —as you’ve just learnt— since the variable becomes local to the auto-loader method it isn’t of much use.

Источник

Оцените статью