The Script Path of an included PHP script? - php

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

Related

PHP include random file from folder

I would like to use a <?php include ... command to choose one random php file from one folder.
So this is the basic idea:
<?php include 'random file: 1.php, 2.php, 3.php or 4.php';?>
I have already read articles like this or this, but they don't answer my question clearly. What is the easiest way to do that?
// Directory to use
$directory = '.';
// Filter out directories, we only want files.
$files = array_filter(scandir($directory), fn($f) => is_file($f));
// Pick a random file
$randFile = $directory . '/' . $files[array_rand($files)];
// Include it
include $randFile;
to include random files from another directory
$path = __DIR__."/folder";
foreach(new DirectoryIterator($path) as $file){
if($file->isFile()){
$arr[] = $file->getFilename();
}
}
$randFile = $path."/".$arr[array_rand($arr)];
include $randFile;

Can not write to php file

I want to make new PHP file and add nav.php but nothing is written on the new file. Only 1 I don't know what it means.
<?php
$video = $_FILES["fileToUpload"]["name"];
$vid_name = $_REQUEST["vid_name"];
$php_name = $_REQUEST["php_name"];
$title_page = $_REQUEST["title_page"];
$vid_location = "klipove/";
//move_uploaded_file($video, $vid_location);
echo $php_name;
$test = "sex-test.php";
//Create new page
file_put_contents($php_name, include "nav.php");
?>
Since you want the contents:
replace file_put_contents($php_name, include("nav.php")); with file_put_contents($php_name, file_get_contents("nav.php"));
Note that contents means the code it self and will not be executed unless you include/require $php_name and $php_name must have .php suffix.

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

Variables not propagating to PHP include file

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.

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.

Categories