problem with using dirname(__FILE__ ,2) in php - php

I try to use dirname(FILE ,2)
I have index.php file in "public" directory
Project
|--app
--config
|--init.php
--public
|--css
--js
--index.php
--resources
|--inc
--langs
--favicon.ico
in index.php
<?php
$pageTitle = "Home";
define('__BASEDIR__', dirname(__FILE__ ,2) );
echo __BASEDIR__ . '<br>';
require_once (__BASEDIR__ . '/config/init.php');
?>
this print: ...\xampp\htdocs\project
in init.php:
<?php
$templ = __BASEDIR__ . '/resources/inc/';
$lang = __BASEDIR__ . '/resources/langs/';
$css = __BASEDIR__ . '/public/css/';
$js = __BASEDIR__ . '/public/js/';
echo $templ .'<br>';
echo $lang .'<br>';
echo $css .'<br>';
echo $js .'<br>';
require_once $templ . 'header.php';
require_once $templ . 'navbar.php';
it shows in the browser
...\xampp\htdocs\project
...\xampp\htdocs\project/resources/inc/
...\xampp\htdocs\project/resources/langs/
...\xampp\htdocs\project/public/css/
...\xampp\htdocs\project/public/js/
Note this ...\project/.... /* backslash */
The browser cannot find files (style.css, script.js....)

Firstly
if the difference in slashes bothers you, you can set your path's using DIRECTORY_SEPARATOR like so
From
$templ = __BASEDIR__ . '/resources/inc/';
to
define('DS', DIRECTORY_SEPARATOR);
$templ = __BASEDIR__.DS.'resources'.DS.'inc'.DS;
Secondly You will not be able to use that relative pathing to load browser resources like js and css files. You need to look into absolute path.
Example: Say your on windows, the path $templ will be something along those lines C:\xampp\htdocs\project\public\css\file.css which will not work for loading your browser resources. If your document root is C:\xampp\htdocs\project\ you will need to use /public/css/file.css to load your css or js files.

Related

How to use wordpress shortcodes in custom php file folder?

I want to display wp formcraft (plugin) in my custom PHP file so I wanted to use "do_shortcode()" function but nothing works it just shows white screen.
my php file code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
define('WP_USE_THEMES', false);
echo apply_filters( "the_content","[fc id='1'][/fc]");
?>
I don't really get what your doing, but i guess you want to do something like this:
function xyz() {
do_shortcode("[fc id='1'][/fc]");
}
echo apply_filters("the_content", "xyz");

How do i set up path and folder pattern in PHP?

So I am trying to set up path for my pdf files that are stored in a folder structure. The file that has to be selected depends on the user input.
I want to set up first the absolute path and then a folder pattern.
The folder where I have stored my files is:
C:\Apache24\htdocs\archivedb\Tourenfahrer\2017\5
The last three folders changes as per user input.
I have setup my root directory in my config.php like this:
define( 'ROOT_DIR', dirname(__FILE__) );
Now in my php file I am recieving my variables, which will be my folder names to search for the files.
<?php
require_once 'conf/config.php';
if (!empty($_REQUEST['magName'] && $_REQUEST['year'] && $_REQUEST['issue']
)) {
$magazineName = $_REQUEST['magName'];
$year = $_REQUEST['year'] ;
$issue = $_REQUEST['issue'] ;
}
Now Please tell how to access the respective folder using these variables?
and how can i set up a pattern with this using glob(); ?
Please try this
define('ROOT_DIR', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
if (!empty($_REQUEST['magName'] && $_REQUEST['year'] && $_REQUEST['issue'])) {
$magazineName = $_REQUEST['magName'];
$year = $_REQUEST['year'];
$issue = $_REQUEST['issue'];
$dir = $magazineName . DS . $year . DS . $issue;
echo "<h3>Use scandir:</h3>";
$files = scandir(ROOT_DIR . DS . $dir);
foreach ($files as $file) {
echo basename($file) . "<br>";
}
echo "<h3>Use glob:</h3>";
foreach (glob($dir . DS . '*') as $filename) {
echo basename($filename) . "<br>";
}
}

How do I include a path of google client library in php

$r = set_include_path(get_include_path() . '\google-api-php-client-master\src');
echo $r;
require_once 'Google/Client.php';
require_once 'Google/Service/AdExchangeSeller.php';
function __autoload($class_name) {
include 'examples/' . $class_name . '.php';
}
when I am running it on browser its showing following error..
You are not using the path seperator when calling set_include_path, try this:
$r = set_include_path(get_include_path() . PATH_SEPARATOR . '\google-api-php-client-master\src');
Here is the documentation on set_include_path
Edit
Did you restart Apache after making the change to your php.ini?

PHP set_include_path statement

I can't quite figure out the meaning of this statement:
set_include_path('.'
. PATH_SEPARATOR . '../library/'
. PATH_SEPARATOR . '../application'
. PATH_SEPARATOR . get_include_path());
A quick breakdown would be appreciated.
It adds the two paths to the include_path so that if you
include a file "../library/filename.php".
you can do it by
include('filename.php');
instead of
include('../library/filename.php');
I suppose this is a part of some framework
It basically adds the folder to the php include path
The first thing to note here is that the constant PATH_SEPARATOR is a predefined constant which allows for a cross-platform path separator (it resolves to ':' on unix-like systems and ';' on windows).
The following code would also achieve the same result but is a bit easier to read:
<?php
$paths = array('.', '../library/', '../application', get_include_path());
set_include_path(join(PATH_SEPARATOR, $paths));
or a bit more verbose, but easy to add to:
<?php
$paths[] = '.';
$paths[] = '../library/';
$paths[] = '../application';
$paths[] = get_include_path();
set_include_path(join(PATH_SEPARATOR, $paths));
What does php's set_include_path function do?
It sets a possible location for the php engine to look for files.
For example:
I put this in a php file called cmp.php under /home1/machines/public_html
<?php
print "1<br>";
require("hello.php");
print "<br>2<br>";
set_include_path("/home1/machines/public_html/php");
print "<br>3<br>";
require("hello.php");
print "<br>4<br>";
?>
Make a new file hello.php under /home1/machines/public_html, put this in there:
<?php
print "hello from public_html";
?>
Make a second new file called hello.php under /home1/machines/public_html/php, put this in there:
<?php
print "hello from public_html/php";
?>
Run cmp.php, and you should get this:

Define absolute path for files

I have the following include files..
require_once ("config.php");
require_once ("functions.php");
require_once ("session.php");
I want to define absolute paths for my include files. I have tried with the following code and no luck..
can you please help to define an appropriate absolute path, so that require_once as expected.
defined('DS') ? null : define('DS',DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null: define('SITE_ROOT',DS.'PHP_Files'. DS . 'phpsandbox'. DS.'my_mat'. DS.'my_test');
defined('LIB_PATH')?null:define('LIB_PATH',SITE_ROOT.DS.'includes');
require_once(LIB_PATH.DS."config.php");
require_once(LIB_PATH.DS."functions.php");
require_once(LIB_PATH.DS."session.php");
These 3 include files in my system are stored in J:\PHP_Files\phpsandbox\my_mat\my_test\includes.
Thanks in advance!
Ok, so I think what you are looking for is the actual system file path. To get that you can echo
dirname( __FILE__ );
You can do this in any file that you want and it will display the system file path relative to your file. For me it's something like this:
/home2/myusername/public_html/project_name/includes/config.php
so if you are interested in the "project_name" folder you should have something like this:
defined("SITE_ROOT") ? null : define("SITE_ROOT", DS . "home2" . DS . "myusername" . DS . "public_html" . DS . "project_name" );
Then if you are looking for the "includes" folder which will be your library you should have something like this:
defined("LIB_PATH") ? null : define("LIB_PATH", SITE_ROOT . DS . "includes" );
Hope this helps. I had the exact same problem and this worked for me.
Cheers, Mihai Popa
Try including your LIB_PATH in your include path.
set_include_path(LIB_PATH . DS . PATH_SEPARATOR . get_include_path());
require_once(dirname(__FILE__).DS."config.php");
require_once(dirname(__FILE__).DS."functions.php");
require_once(dirname(__FILE__).DS."session.php");
check it out , i think it's good
You need realpath function. Also, you can get all files included by get_included_files that returns array of absolute paths of files you've included at the moment function's got called.
defined('DS') or define('DS',DIRECTORY_SEPARATOR);
$disk_label = '';
if (PHP_OS == 'WINNT') {
if (FALSE !== ($pos = strpos(__FILE__, ':'))) {
$disk_label = substr(__FILE__, 0,$pos+1);
}
}
defined('SITE_ROOT') or define('SITE_ROOT', $disk_label.DS.'PHP_Files'. DS . 'phpsandbox'. DS.'my_mat'. DS.'my_test');
defined('LIB_PATH') or define('LIB_PATH',SITE_ROOT.DS.'includes');
require_once(LIB_PATH.DS."config.php");
require_once(LIB_PATH.DS."functions.php");
require_once(LIB_PATH.DS."session.php");

Categories