Variables not propagating to PHP include file - php

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.

Related

My require method does not require my php file in my second file

My first file
conf.inc.php
<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
$ip_blocked = array('127.0.0.1', '100.100.100.100');
?>
My second file
index.php
<?php
require conf.inc.php;
echo $ip_address;
?>
My require does not require the conf.inc.php files and it kills the page since it does not get the files. Can anyone help?
Your filename should be a string. Try to use require 'conf.inc.php';

How can I use PHP to grab files from different directories

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;
?>

php gettext included from different folders doesnt work?

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";

PHP - Randomly grab a file from folder and echo

I have a folder on my server called /assets/includes/updates/ with .php files inside featuring static html content.
I'd like to randomly grab a file from this folder and echo it into a div. Here is what I have:
<?php
function random_update($dir = $_SERVER['DOCUMENT_ROOT'].'/assets/includes/updates/')
{
$files = glob($dir . '/*.*');
$file = array_rand($files);
return $files[$file];
}
?>
<div class="my-div">
<?php echo random_update(); ?>
</div><!--end my-div-->
I am getting 500 errors? Also, my intention is to only echo 1 file at a time. Will the provided code accomplish that?
Php does not recognize the syntax you used. You have to bypass it like this:
<?php
function random_update($dir = NULL)
{
if ($dir === NULL) {
$dir = $_SERVER['DOCUMENT_ROOT'] . '/assets/includes/updates/';
}
$files = glob($dir . '/*.*');
$file = array_rand($files);
return $files[$file];
}
Also, you might want to enable error dumping in your development environment so you know what went wrong next time.
Aside from another answers spotted issues, for your code to do what you want, you have to replace your following code:
<?php echo random_update(); ?>
for this one:
<?php echo file_get_contents (random_update()); ?>
because your current code will print the filename inside the div, while I think you wanted the actual content of the file to be inserted in the div.
You can't use any expression as "default" function's argument value.

The Script Path of an included PHP script?

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__);

Categories