why do i get 1 instead of the file required? [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
including php file from another server with php
i want to have my own error system instead of having the php errors so for example i require a file from another server and that server is not available right now
<?php
require 'http://example.com/file.php' or die ("that host is not available right now");
?>
but instead i get
Warning: require(1) [function.require]: failed to open stream: No such
file or directory in C:\xampp\htdocs\index.php on line 5
Fatal error: require() [function.require]: Failed opening required '1'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\index.php on line 5

It's because require 'foo' or bar() is interpreted as require ('foo' or bar()). 'foo' or bar() equals true, i.e. 1. If you want to write it like this, use different parentheses:
(require 'http://example.com/file.php') or die ("that host is not available right now");
But, you don't need die at all here, since require will already halt program execution if the required file can't be loaded. Just require 'http://example.com/file.php'; will do fine. Whether you should actually load foreign PHP files over the network is another story (hint: probably not).

The problem is the precedence of operators. The PHP are including the result of the "or" comparison (true). Try to remove this.
include('http://...') or die('error...');
It will work.

Related

Use php require for local directory

I have recently been trying to add PHPMailer script to my site, following the accepted answer from here:
Send attachments with PHP Mail()?
I have never used a require statment before so I assume I must be doing it wrong, currently testing on my local host version of the site. I have downloaded the entire zip of git (unzipped it) and moved it to the dir my scripts are in.
In my script I added the following line:
require_once(__DIR__.'/PHPMailer-master/class.phpmailer.php');
Which to my understanding was all I needed to do to use it. However I get this error:
Warning:
require_once(C:\xampp\htdocs\Website\PHPMailer-master\class.phpmailer.php):
failed to open stream: No such file or directory in
C:\xampp\htdocs\Website\include\mailer.php on line 10
Fatal error: require_once(): Failed opening required
'C:\xampp\htdocs\Website\PHPMailer-master\class.phpmailer.php'
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\Website\include\mailer.php on line 10
Any ideas what is causing this error? Am running PHP 5.6.21 if this is relevant.
Try to use the full path to the file, using realpath() - http://php.net/realpath, and use dirname(__FILE__) to get your current directory:

Soap/php/lighttpd server errors, can not find file that is there?

I'm sticking a web interface which I've been given (that works on other systems) onto a switch, which happens to be in php/soap.
I get this error
Warning: require(/class_main.php) [function.require]: failed to open stream: No such file or directory in /jffs2/usbflash0/ran/www/includes/config.php on line 4
Fatal error: require() [function.require]: Failed opening required '/class_main.php' (include_path='.:') in /jffs2/usbflash0/ran/www/includes/config.php on line 4
config.php:
<?php^M
require($_SERVER['DOCUMENT_ROOT'].'/includes/errors.php');^M
require($_SERVER['DOCUMENT_ROOT'].'/includes/library.php');^M
require(get_structures_directory().'/class_main.php'); ^M
error_log(get_structures_directory());
require(get_structures_directory().'/class_softcore.php');^M
require($_SERVER['DOCUMENT_ROOT'].'/includes/format.php');^M
?>^M
Which looks for class_main.php in here:
1.0.1/ 1.0.3/ 2.0.5/ 2.0.7/ 3.0.10/ 3.0.12/ 3.0.3/ 3.0.5/ 3.0.7/ 3.0.9/
1.0.2/ 1.0.4/ 2.0.6/ 3.0.1/ 3.0.11/ 3.0.2/ 3.0.4/ 3.0.6/ 3.0.8/
Now class_main.php is in some of these version folders and not in some of them. What is the issue here? is it trying to use one of the folders where it is not located?
Well First class_main.php must be in all the folders, So application will not fail for any instance. If it is unlikely you can check for version and require only in that cases like
switch($version)
{
case "1":
case "2":
require($file_path);
break;
}
You can also use include instead of require which will give you warnings instead of error, However it is unadvisable specially on pure php libraries, As it will gave application undefined behaviour.

require_once is unable to find the file

Hello i have a stupid question i search in stackflow there was lots of answeserd but i couldnt find solution for my problem i want to call a file in php receiving error actually the code is correct but i am receiving a problem error which is:
Warning: require_once(1) [function.require-once]: failed to open stream:
No such file or directory in C:\xampp\htdocs\Upload images\upload.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '1'(include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\Upload
images\upload.php on line 2
I am using dreamweaver. i deleted the path with directory and recreated with different name but not solved still prob exist. also i did google there also i didn't find solution ??????? any one please my head is exploding....??????????????????????????????
A quick test on my system reveals that require_once("file") or error_function(); fails with that exact message; require_once("file"); does not.
This is possibly because require_once is a statement rather than a function, and might have different parsing rules, causing your statement to actually be require_once('db.php' or mysql_error());. The PHP or operator with those arguments will return TRUE without calling mysql_error, causing the statement to be require_once(1) (1 == TRUE) - which would generate those messages. This is just my speculation.
tl;dr
Try removing the or mysql_error() from your code. require_once will generate a fatal error anyway. (Warning: Works On My Machineā„¢.)
Try write complete path in require_once(path)

Two Errors trying to include .php file: Warning: require(...) AND Fatal error: require() ... (include_path='.;C:\php\pear')

Here is the first error:
Warning: require(/hb/includes/constants.php) [function.require]: failed to open stream: No such file or directory in C:\wamp2\www\hb\includes\connection.php on line 2
And the second error that immediately follows:
Fatal error: require() [function.require]: Failed opening required '/hb/includes/constants.php' (include_path='.;C:\php\pear') in C:\wamp2\www\hb\includes\connection.php on line 2
Here is the line that it doesn't like:
require("/hb/includes/constants.php");
I did install a new version of WAMP, but I know the directories/files are fine and the code worked previously and on another server. It seems like none of my require()'s are working anymore. I tried looking around and some suggested to others that they edit their php.ini, but I don't know what directory I would point to - if you can't tell I'm pretty noobish regarding all things server related.
Any help is appreciated.
First of all, are your really -- really ! -- sure that the file /hb/includes/constants.php, that your are trying to include, exists ?
Considering that the path you requested begins with a /, it looks like you're using an absolute path... that looks like an UNIX path, but you are on a Windows server...
If it exists, and PHP still cannot include it, check :
that it is readable by your webserver (including the directories) -- I'm thinking about UNIX permissions
that there is no open_basedir restriction that would prevent PHP from accessing those files.
BTW, having a warning and a fatal error is the behavior that's to be expected when using require : the warning is normal (it comes from include) ; and the fatal error is the difference between include and require.

Another PHP require_once issue [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
require_once () or die() not working
I try to include a file with require_once and i get:
Warning: require_once(1) [function.require-once]: failed to open stream: No such file or directory in /var/www/phpscrape/index.php on line 3
Fatal error: require_once() [function.require]: Failed opening required '1' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/phpscrape/index.php on line 3
I can do a cat /var/www/phpscrape/simplehtmldom/simple_html_dom.php which means the path is correct.
The permissions are www-data:www-data same as index.php which is executing. To be sure i also set chmod 777 but that didnt help either.
The command line is:
<? php
require_once("/var/www/phpscrape/simplehtmldom/simple_html_dom.php") or die("no such file");
You are hitting a weird side effect of the fact that require is not a real function, but a language construct.
require does not require its parameter to be wrapped into parentheses.
Therefore, what require interprets as the parameter is the full rest of the line:
(("/var/www/phpscrape/simplehtmldom/simple_html_dom.php") or die("no such file"))
which equals 1.
I would either just drop the die() (require will die on error anyway) or do a file_exists() check beforehand.
require_once("var/www/phpscrape/simplehtmldom/simple_html_dom.php") or die("no such file");
Without the / at the beginning
If not working try:
require_once("phpscrape/simplehtmldom/simple_html_dom.php") or die("no such file");

Categories