file_get_contents to url on the same server - php

I have two files on my server.
File a.php:
<?php
die('this is my text');
?>
File b.php:
<?php
file_get_contents('http://mysite.pl/a.php');
?>
But it's not working... I can't use file_get_contents, when the files are located on the same server, I don't know why.
PHP Info:
allow_url_fopen: ON
allow_url_include: OFF
When I try use the code from file b.php on difficult server - it working... ;/

You could try using 127.0.0.1, however it won't work on a VirtualHost.

You can try the relative path.
If a.php and b.php are in the same directory, do:
// b.php (If a.php is in the same directory)
file_get_contents('./a.php');
If a.php is in the upper directory:
// b.php (If a.php is in an upper directory)
file_get_contents('../a.php');
If a.php is in the root directory:
// b.php (If a.php is in the root directory)
file_get_contents('/a.php');

Related

PHP include file that includes a file up the directory tree

I have this directory tree:
test.php
foodir
`---- foo.php
bardir
`---- bar.php
When I open test.php, I include foo.php. Then, I want foo.php to include bar.php.
test.php:
include 'foodir/foo.php';
foo.php:
include '../bardir/bar.php';
However, when I open test.php, I get:
Warning: include(../bardir/bar.php): failed to open stream: No such file or directory
I noticed that if I change my directory tree to:
test.php
foodir
`---- foo.php
---- bardir
`---- bar.php
And then change foo.php to:
include 'bardir/bar.php';
Everything works. It appears that I can include files relative to the currently included file.
However, why am I not able to travel up the directory tree of that file?
Edit:
I know that I can put include 'bardir/bar.php' in foo.php. It would search for bar.php in the location of test.php. However, that doesn't solve my problem if I include foo.php from a file in a directory other than the one where test.php is. That's because in that other directory, bardir/bar.php wouldn't exist.
When you use include 'foodir/foo.php'; that file is now basically running in the "scope" of test.php.
Therefore, the include inside that file include '../bardir/bar.php';
will go up one folder and THEN searching for bardir, which isnt there:
(it's looking for this arrangement:)
parent/test.php
parent/foodir
`---- foo.php
bardir
`---- bar.php
so, the correct include to use inside foo.php would be include 'bardir/bar.php';
in short: If you open test.php all include-paths used in any included file should be relative to the location of test.php - not to their actual location.
ps.: As mentioned in the comments: Includes are first checking the defined include-dir, only if there isn't a match, the path is considered "relative".
Simply do:
include dirname(__DIR__).'/bardir/bar.php';
__DIR__ is the absolute directory path of the file where this constant is used/called (no matter where it's included from), thus __DIR__ is equal to /full/absolute/path/foodir. and dirname(__DIR__) goes one directory up of the path so dirname(__DIR__) /full/absolute/path, and where the bar.php resides is /full/absolute/path/bardir. That's what we want, that's what we get.
This way you don't have to worry include path relativity. It will work any case, no matter where foodir/foo.php is included from
"When you use include it assume the base directory to look for
file is the directory of current file"
So when you have to include file from the another dir you have to specify the relative path from parent directory as here "Base" to include so let say for
Base -> A
->file1.php
->B
->file1.php
we will use
include "../A/file1.php";
for Adding files from A and vice-versa.
Assume ../ as the one level up of specified dir
also you can use __DIR__ like build in constants for getting the dir name of current file

PHP file path error for autoloader

Here is my file structure on my cpanel webserver
root
> vendor
> autoload.php
> public_html
> folder
> file2.php
> script_folder
> include_file.php
> file1.php
Inside include_file.php I have
require_once('../vendor/autoload.php');
file1.php and file2.php both contain the same call to include_file.php
require_once($_SERVER["DOCUMENT_ROOT"]."/script_folder/include_file.php");
This works fine when I run file1.php but when I run file2.php i receive the following error message.
No such file or directory Fatal error: require_once(): Failed opening required '../vendor/autoload.php' (include_path='.:/opt/cpanel/ea-php71/root/usr/share/pear') in /lvl1/lvl2/public_html/script_folder/include_file.php
However, if I change require_once('../vendor/autoload.php'); to require_once('../../vendor/autoload.php'); in include_file.php then file2.php works and file1.php does not work. It showes a similar error.
I understand this is a file path issue but, what I don't understand is why. Shouldn't the path in include_file.php always be the same no matter what file is calling it; i.e. file1.php or file2.php?
The way I see it is the the actual require_once statement is being called from include_file.php but, the behavior I'm seeing makes me think the require_once statement is being ran from file1.php or file2.php resulting in the filepath error.
Can someone please clarify?
UPDATE: inside include_file.php I have tried using:
require_once($_SERVER["DOCUMENT_ROOT"] . '/vendor/autoload.php');
and
require_once(dirname(__FILE__).'/vendor/autoload.php');
neither of these work since both return public_html as the main working directory. My vendor folder is outside the main working directory.
what is returned is this:
/script_folder/vendor/autoload.php
I understand I can simply include the correct file path at the beginning of file1.php and file2.php but, I was trying to figure out a way to reduce the number of requires I need in each file to only one by pointing them to include_file.php, then letting include_file.php do the rest of the work.
My include_file.php file contains several other require statements to other scripts on my server. Kind of like a mini autoloader. All of the other require statements work fine except the autoload.php one I'm having trouble with here.
The only difference I can see is my other scripts are within my public_html folder and my autoload.php file is located outside of public_html.
Assuming you are using Composer, you only need to require "autoload.php" once in file1.php and the file should contain a namespace like namespace App; at the top of the file below the opening PHP tag. Subsequently, file2.php should contain namespace App\folder; and included_file.php should contain namespace App\script_folder;.
The namespace should also be defined in composer.json like so:
"autoload": {
"psr-4": {
"App\\": "public_html/"
}
}
This seems to work perfectly. Guess instead of a relative path I need to use the absolute path.
include_file.php:
$autoload = str_replace('public_html','vendor/autoload.php',$_SERVER["DOCUMENT_ROOT"]);
include($autoload);
it allows me to call:
require_once($_SERVER["DOCUMENT_ROOT"]."/script_folder/include_file.php");
from any files that are in my public_html folder.
in include_file.php $_SERVER["DOCUMENT_ROOT"] returns /lvl1/lvl2/public_html (the full server path to the publicly visible folder)
then I replace public_html with the the path to my autoloader vendor/autoload.php

php included files and directories

Lets say I have two files in directory new_folder: a.php and b.php
In the file of a.php there is a statement:
include_once 'b.php'
In addition, I have a file demo.php in a parent folder, in the demo file there is a statement:
include_once 'new_folder/a.php'
I.E. the folder structure is:
demo.php
new_folder
- a.php
- b.php
Why if I write in the a.php file:
include_once 'b.php' - correct path
include_once './b.php' - incorrect path
include_once 'new_folder/b.php' - incorrect path
Try This
require_once('../b.php');
my answer is based on php.net:
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
that means that if I write in a.php: include_once 'b.php' ; and then include a.php in demo.php then first it looks for the file in the include_path, then it will look for the file in the directory of the calling script (a.php), and only then it will look for the file in the working directory (that is the directory of the file that we run and starts all the includes- demo.php)
edit: I tried running it and found out that first if there is b.php in the working directory then it will include it and not the b.php from the calling scrip's own directory.
the last check for the file is in the new_folder (the calling script's own directory)

what am i doing wrong in these php scripts?

Here is config.php
<?php
define("DB_SERVER","localhost");
define("DB_NAME","photo_galley");
define("DB_USER_NAME","root");
define("DB_PASSWORD","");
echo DB_SERVER."config file<br>";
?>
Here is classes.php
<?php
var_dump(include_once("config.php"));
echo DB_SERVER."class file<br/>";
?>
<?php
class MySQLDatabase{...
and here comes third file temp.php
<?php
include_once("includes/classes.php");
echo DB_SERVER." tem.php <br/>";
?>
both classes.php and config.php are in includes folder, includes folder is in photo_gallery folder , temp.php is in photo_gallery folder
how it looks like when i open classes.php in browser.
it shows thing as i thought. First show DB_SERVER value from config.php file then from classes.php file
But when i include classes.php file in any other file like in temp.php as shown in third piece of code it gives something that i didn't understand
here is it
I didn't understand output.
I think it's output should be same as that of classes.php with one more line showing DB_SERVER value from temp.php.
Would you please tell me why these constants are shown as undefined when i open temp.php in browser?
if include function in line is successful as shown by int(1) then why not it show DB_SERVER value from config.php?
You include config.php from wrong location in temp.php. Remove the path. Also you should use require_once rather than include_once as config.php is crucial, so lack of it should stop the script.
In temp.php your config.php file path is wrong. Thats why you got the undefined constant.
config.php file inside the includes directory. But your temp.php file outside the includes directory.
in classes.php, You can use $_SERVER['DOCUMENT_ROOT'] add full path in such way you can access the same in all files.
Your scripts are fine. You just have a problem with your server configuration.
They're working correctly in mine:
When you include classes.php relative path to config.php is includes/config.php.
<?php
include_once("includes/config.php");
class MySQLDatabase{...
so we can say that when we use require include function it just copies code from file mentioned to file from which this function called. so when i call config from classes file it works fine. But when i call classes from temp it just copies code from there but for temp file config file location is different so it doesn't work. Am i correct Mr. Developers...

PHP require_once or include_once nesting

Directory structure,
A.php
folder
B.php
C.php
A.php includes B.php and B.php includes C.php.
But B.php needs to include C.php relative to A.php. Is there any way I can include C.php relative to B.php?
You can include using __DIR__ or dirname(__FILE__).
From the manual
The directory of the file. If used
inside an include, the directory of
the included file is returned. This is
equivalent to dirname(__FILE__).
try this
A.PHP
include_once(__DIR__.'/folder/B.php');
include_once(__DIR__.'/folder/C.php');
B.php
include_once(__DIR__.'/A.php');

Categories