In one of my magento template phtml files I am trying to include a seperate php file.
When I include it I get nothing outputted and when i use require instead I get the following error
Fatal error: require(): Failed opening required 'http://www.site.co.uk/dir/test.php'
(include_path='/home/usr/public_html/app/code/local:/home/usr/public_html/app/code/community:/home/usr/public_html/app/code/core:/home/usr/public_html/lib:.:/usr/lib/php:/usr/local/lib/php') in /home/usr/public_html/app/design/frontend/theme/edits/template/review/product/view/list.phtml on line 30
The first line of the error shows the correct url path and when i go to it directly it works - it just doesn't like being included/required from the phtml template page.
I've tried the following in the phtml file (using magento's BaseURL, absolute path and the relative path):
<?php
$root = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
require/include ($root.'dir/test.php');
?>
<?php
require/include ('http://www.site.co.uk/dir/test.php');
?>
<?php
require/include ('../../../../../../../../../dir/test.php');
?>
Instead of Mage::getBaseUrl use $root = Mage::getBaseDir();
require('http://....') is going to (if it's enabled) do a full-blown HTTP request to the specified URL. Since it's a URL, that webserver is going to EXECUTE the php script and send you its output.
If you're trying to actually load the CODE of that test.php, e.g. the raw un-executed PHP, then you cannot use an http request. The remote server has NO idea that the http request is actually from PHP and is coming from in include(). It'll just blindly run that script and send over the output.
You'd need to (say) rename the remote file to test.txt, so that the webserver sends it out as-is, and then that text will be executed as PHP code on YOUR server.
And as far as the other paths go, if your $root is something like:
/home/sites/example.com/html
then the require is going to be looking for
/home/sites/example.com/html/dir/test.php
Is there a dir subdir inside whatever your site's $root is?
Related
I need to use file_get_contents() to get the content of a html page (content.html) in a file (file.php) that is in a directory like this:
/content
- content.html
/functions
- file.php
So I tried to use file_get_contents('../content/content.html') to get the html content, but i get this error:
failed to open stream: No such file or directory
I think I might be using the path wrong. What can I do?
Use $_SERVER['DOCUMENT_ROOT'] all the time, so you won't have to deal problematically with the file paths.
Try:
file_get_contents($_SERVER['DOCUMENT_ROOT'] .'/content/content.html');
I think it's better to use __DIR__ here instead on relying on a variable set by the web server :
file_get_contents(__DIR__ . '/../content/content.html')
$_SERVER['DOCUMENT_ROOT'] is error prone as it relies on the webserver (Apache in this case), if you use Nginx instead your code may break as you have to configure it to set this variable...., instead I prefer relying on __DIR__ which is provided by PHP thus environment agnostic.
I know this error is dicussed often already, but I just can't use require or include in my function.php(xampp) without this error on the top and I don't have a php.ini file. I don't even want one, if not necessary!
I just want to include a second php file in the fuction.php. Come on that can't be so hard...
That's what I used.
require_once (get_stylesheet_directory_uri().'/index.php');
(result: http://localhost/werkstatt/wp-content/themes/twentysixteen/index.php)
I mean in this case the index.php would be inluded anyway which leads me to the second question. Why or how are some standard files like index.php already inluded?
index.php is not included at all, this is the entry point to PHP code execution and it is defined in your webserver config (not really precise, but should give you an idea).
The PHP file you want to include, is it on the same server? If so, you need to include it by its filename, and not by its URL. I understand you want to include another PHP file which is part of your theme.
I don't know much about wordpress and what helper functions it defines, but this seems to be the one you need to use: https://developer.wordpress.org/reference/functions/get_template_directory/.
If it is not placed an the same server, then you just should not do this.
I have a problem with an URL-include, which I don't understand...:
For testing I have coded the following script:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
include("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text";
?>
Allow_url_include is set to on. (via php.ini)
Allor_url_fopen ist set to on. (via php.ini)
The includetest.php only contains plain text for testing. There is no php-code.
The result of that script is only the "first text". After that the script is stopped.
If I use "or die('not working');" after the include, the result is the whole text (also the second text) with the following warning:
Warning: include(1): failed to open stream: No such file or directory
in /srv2/www/htdocs/xhtml-test/_baustelle/testphp02.php on line 6
Warning: include(): Failed opening '1' for inclusion
(include_path='.:/usr/share/php:/usr/share/pear') in
/srv2/www/htdocs/xhtml-test/_baustelle/testphp02.php on line 6
Why is that? I am at a loss...
Here is the problem of code:
// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';
Ref. of this Is Here
The file you are including is not a valid php file as it is already surved by a server as php.
This code should work as you want:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
echo file_get_contents("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text";
?>
You should use Relative paths in PHP include function.
include '/path/to/file.php'; // You can include file by relative path
As per documentation,
include through HTTP
If "URL include wrappers" are enabled in PHP, you can specify the file
to be included using a URL (via HTTP or other supported wrapper - see
Supported Protocols and Wrappers for a list of protocols) instead of a
local pathname. If the target server interprets the target file as PHP
code, variables may be passed to the included file using a URL request
string as used with HTTP GET. This is not strictly speaking the same
thing as including the file and having it inherit the parent file's
variable scope; the script is actually being run on the remote server
and the result is then being included into the local script.
/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */
// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';
$foo = 1;
$bar = 2;
include 'file.txt'; // Works.
include 'file.php'; // Works.
Warning
Security warning
Remote file may be processed at the remote server (depending on the
file extension and the fact if the remote server runs PHP or not) but
it still has to produce a valid PHP script because it will be
processed at the local server. If the file from the remote server
should be processed there and outputted only, readfile() is much
better function to use. Otherwise, special care should be taken to
secure the remote script to produce a valid and desired code.
Here is understanding of Paths.
1) Relative Paths
index.html
/graphics/image.png
/help/articles/how-do-i-set-up-a-webpage.html
2) Absolute Paths
http://www.mysite1.com
http://www.mysite2.com/graphics/image.png
http://www.mysite3.com/help/articles/how-do-i-set-up-a-webpage.html
The first difference you'll notice between the two different types of links is that absolute paths always include the domain name of the website, including http://www., whereas relative links only point to a file or a file path. When a user clicks a relative link, the browser takes them to that location on the current site.
For that reason, you can only use relative links when linking to pages or files within your site, and you must use absolute links if you're linking to a location on another website.
For more information, Refer this link also.
Hope it will help you :)
I need get full path to my web-project directory, as I am using single entry point so I doing this:
$_SERVER['SCRIPT_FILENAME']
then remove index.php and then I get something like this: /var/www/myproject, and then I can use it for require some stuff files like configuration etc. But when I call this method (e.g. for include some stylesheets) in my php files for rendering I getting 404 error for this stylesheet and what I getting in browser console:
http://localhost/var/www/myproject/bower_components/bootstrap/dist/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)
Obviosly that path should looks like:
http://localhost/myproject/bower_components/bootstrap/dist/css/bootstrap.min.css
So the question is how I can properly get path to my project for proper include files in PHP and load assets in HTML ? And the approach what I am using now it ok? Or I need to do somehow better? Thanks!
The problem is there are TWO type of adresses:
1) LOCAL, which are accessible throw your scripts from INSIDE your server.
You use them if need to include file inside your script and such, but not from outside by browser.
2) WEB addresses, which are accessible by browser from OUTSIDE.
You use them if need to give some file to browser, like style-sheet or JavaScript script.
Try $_SERVER['SERVER_NAME'] . $_SERVER["DOCUMENT_ROOT"] for this task.
Detailed description for server environment variables
I am working on PHP with AJAX. Because of AJAX, one can not say that in which directory you are at in a particular point of time. So to call files I can't use a fixed path and filename. So, I want to give path starting from the root to the file. It is working well.
But the problem comes if I want to include a file. Say I want to include a file test.php like:
include_once("http://localhost/sms/test.php");
The file is included but the problem is:
<?php
$i = 9;
include_once("http://localhost/sms/test.php");
?>
test.php contains,
<?php
echo $i;
?>
This code should give output as:
9
...but it gives nothing. I know the reason: the browser is requesting the server via separate HTTP request because path contains "http" so server returns HTML output. so $i doesn't exist: no output.
How do I call files in include using their path from root to run AJAX properly?
try require_once($_SERVER['DOCUMENT_ROOT'] . '/sms/test.php');
You should never use hostnames in local file paths. Instead, use $_SERVER['DOCUMENT_ROOT'], like this:
require_once("{$_SERVER['DOCUMENT_ROOT']}/sms/test.php");
This will append something like /var/www (or whatever the root path is) to the start of the file path. Also, using require_once() instead of include_once() is better; if PHP can't find the file to include, it will say so and stop execution, instead of possibly failing silently.