I understand in HTML, you can use ../../../ to include content which is included in other folders.
I have this PHP code and I'm wondering how can I use ../../../ before header.php?
<?php
ob_start();
include("../../../header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","Homepage",$buffer);
echo $buffer;
?>
you can use chdir() php function and getcwd(). im suggest you use constant as source path location..
try this:
<?php
ob_start();
chdir("../../../");
$cwd = rtrim(str_replace("\\", "/", getcwd()), '/').'/';
include($cwd."header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","Homepage",$buffer);
echo $buffer;
?>
Related
i am trying to localize my code and i want to use gettext and poedit to do so it seems like the most straight forward way to do it.
I initialize all my classes and views from a simple script which includes this.
I am struggling to understand how the gettext() function works here is an example of what i mean:
File structure of the translation from root: i18n/Locale/da_DK/LC_MESSAGES.
The messages.po and .mo is in the LC_MESSAGES folder and if my scripts are in the i18n folder and i call c.php in the browser it works
If i put the c.php in a folder further apart it doesn't work so the path is: someFolder/i18n/Locale/da_DK/LC_MESSAGES
and the c.php is in someFolder and includes a.php and b.php from i18n.
scripts:
a.php
<?php
// use sessions
session_start();
// get language preference
if (isset($_GET["lang"])) {
$language = $_GET["lang"];
}
else if (isset($_SESSION["lang"])) {
$language = $_SESSION["lang"];
}
else {
$language = "da_DK";
}
// save language preference for future page requests
$_SESSION["Language"] = $language;
$folder = "Locale";
$domain = "messages";
$encoding = "UTF-8";
putenv("LANG=" . $language);
setlocale(LC_ALL, $language);
bindtextdomain($domain, $folder);
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
b.php
<?php
echo _('Change language');
?>
c.php
<?php
include('a.php');
include('b.php');
?>
c.php (in someFolder)
<?php
include('i18n/a.php');
include('i18n/b.php');
?>
Most probably it's all because of your Folder definition:
$folder = "Locale";
When you move your file to someupper folder, you should modify path to something like:
$folder = "someFolder/Locale";
Let me know if this helps.
EDIT:
Or even better, hardcode your path if you know it:
$folder = "/home/me/myproject/someFolder/i18n/Locale";
I have a directory tree like the below
root
index.php
/inc
1levelData.php
1level.php
/images
db.png
What I am trying to figure out is a way I can set an absolute path that can be used for both including files and images. In addition, a method that will work when loading both index.php as well as root\inc\1level.php I have put in some sample code below for what I am currently working with. This code seems to work well for include() but doesn't function for images. Is there a possible fix?
root/Index:
<?php
$fullPath = realpath($_SERVER['DOCUMENT_ROOT']);
echo "<img src = '".$fullPath."/inc/images/db.png'><BR>";
include $fullPath.'/inc/1levelData.php';
?>
root/inc/1levelData.php
<?php
echo "<img src = '".$fullPath."/inc/images/db.png'><BR>";
include ($fullPath.'/inc/images/2levelData.php');
?>
root/inc/1Level.php
<?php
$fullPath = realpath($_SERVER['DOCUMENT_ROOT']);
include ($fullPath.'/phpinfo.php');
?>
root/inc/2LevelData.php
<?php
echo "<img src = '".$fullPath."inc/images/db.png'><BR>";
?>
I have a file called index.php and in this file there is a variable called $mainDir, I would like this variable to be used in the file that is included,
Example:
<?php // index.php
$mainDir = dirname(__FILE__);
if ($isTest){ // presume this is true
include $mainDir . 'test.php';
}
?>
// Different File:
<?php // test.php
echo $mainDir;
?>
I have looked at the other solutions and I have placed the variable I want before the include statement and the include statement is being called properly, it just gives me a warning that the variable that I want to use in the included file is not set.
dirname(__FILE__); doesn't include a "/" at the end I believe. To fix this, simply use this as your include:
include $mainDir.'/test.php';
Maybe you can try this :
<?php // index.php
$mainDir = dirname(__FILE__);
if ($isTest){ // presume this is true
include $mainDir . 'test.php';
}
$_GET["mainDir"] = $mainDir;
?>
// Different File:
<?php // test.php
echo $_GET["mainDir"];
?>
it's horrible but it should works.
I am trying to build my own simple template handler to easily be able to nest and combine files into variables and call these variables when neccessary, as shown below:
$partial['header'] = 'header.php';
foreach($partial as $part => $view ) {
$output[$name] = file_get_contents( APPPATH .'views/' . $view . '.php' );
}
extract($output, EXTR_PREFIX_ALL, 'template' );
include 'mytemplate.php';
The mytemplate.php template file:
<?php
echo $template_header; // Shows the header.
The question:
Obviously, loading a PHP file by file_get_contents isn't going to call any PHP code inside the loaded file and I am sure that there's better options available then using eval. What should I change to be able to use PHP inside my template files?
More ugly is possible but doing exactly what you're wanting :
function custom_get_content($filename){
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
}
Then you can call it :
foreach($partial as $part => $view ) {
$output[$name] = custom_get_content( APPPATH .'views/' . $view . '.php' );
}
I copied that from a comment in the PHP manual but can't find it anymore (and that's why maybe it's too ugly :D)
Reading the scripts in is obiously not the best approach. You'll have to eval them.
And just for the record:
include($filename);
Is functionally identical to:
eval("?>" . file_get_contents($filename));
Get over it. The "eval is evil" meme is just that, a meme. So if you want to keep your appraoch, you could just use eval("?>$template_header"); instead of echo.
The alternative would be to skip the file reading, and have your $template_vars contain filenames rather than their content. Then do an ordinary include($template_header);
Is there a way to achieve the following?
In my /www/var/public_html/index.php file i have this
<?php include('database/connect.php'); ?>
And then in /www/var/public_html/database/connect.php, I want to do something like this
<?php
$my_path = get_path_of_current_script(); // should not be path of index.php
echo $my_path;
// should print
// /www/var/public_html/database
?>
I don't want $my_path to print /www/var/public_html/
Is there a PHP function to do this?
$my_path = dirname(__FILE__);