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:
Related
my script file path is:
C:\xampp\htdocs\wordpress\wp-content\plugins\test\test.php
I need to run code from this path that will move images from path:
C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04
to:
C:\xampp\htdocs\wordpress\wp-content\uploads\images
My problem is that I have no idea how to force "rename" go two directories back in path.
I was trying something like:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
error I'm getting atm (I've changed my folder permissions, so maybe it is something with path?):
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied
. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
edit rename code:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
$destPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/images');
/*$srcDir = opendir($srcPath);*/
echo $srcPath ;
sleep(1);
rename($srcPath, $destPath);
From looking at it, it looks wrong
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
it should be
$srcPath = realpath(dirname(__FILE__) . '../..' . '/uploads/2017/04/obrazek.png');
You have an extra / slash at the beginning.
It seems that you're in a Windows machine, so you're using the wrong type of slashes: /../..' . '/uploads/2017/04/obrazek.png'
You could try to replace them with backslashes:\..\..\uploads\2017\04\obrazek.png'
Or you could try something like this:
$path = substr($srcPath, 0, strrpos($srcPath, '\20'));
// $path now contaits: 'C:\xampp\htdocs\wordpress\wp-content\uploads';
// Then you can do:
$srcPath = $path . '\images';
Regarding the warning error:
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
It seems that you try to rename a file to directory, so probably you forgot to append the file name to the new path.
$srcPath contains a file. $destPath contains a directory, but it should contain the file name.
I've finally after many hours.. find out how to fix it so here you go:
$srcPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\2017\04') . '\obrazek2.png');
$destPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\images') . '\obrazek2.png');
rename ($srcPath , $destPath );
The key was adding file name . '\obrazek2.png' after dirname (), because if filename is inside dirname () and it is destination path in rename, then it returns empty... :)
Not sure if I'm clear enough, because of my English, but it works and hopes it will help someone.
$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?
I'm getting the following error with wordnik's php api
Fatal error: Class 'Example' not found in Swagger.php on line 212
I've edited Swagger.php to get it to work, below is the original function
function swagger_autoloader($className) {
$currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
if (file_exists($currentDir . '/' . $className . '.php')) {
include $currentDir . '/' . $className . '.php';
} elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
include $currentDir . '/models/' . $className . '.php';
}
}
The working code I changed to is
function swagger_autoloader($className)
{
include $currentDir . '/models/' . $className . '.php';
}
MORE INFO
My file structure is as follows.
WORDNIK (contains start.php)>>>WORDNIK (contains Swagger.php)>>MODELS (contains Example.php)
I'm using wampserver 2.2 with php 5.4.3
UPDATE/EDIT
Using the following code
function swagger_autoloader($className) {
echo "dirname(__FILE__)= ",dirname(__FILE__);
$currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
echo "currentDir=".$currentDir."</br>";
echo "className=".$className."</br>";
echo "__FILE__=",__FILE__."<br/>";
die();
I get the results
dirname(__FILE__)=D:\wamp\www\wordnik\wordnik
currentDir=
className=Example
__FILE__=D:\wamp\www\wordnik\wordnik\Swagger.php
As suggested by vcampitelli, using either __DIR__ or dirname(__FILE__) works.
MY QUESTION
Why doesn't the original function work?
UNIMPORTANT INFO
For people struggling through the examples, here is my start.php file
<?php
require('./wordnik/Swagger.php');
require('./wordnik/WordApi.php');
require('./wordnik/AccountApi.php');
require('./wordnik/WordsApi.php');
require('./wordnik/WordListApi.php');
require('./wordnik/WordListsApi.php');
$myAPIKey = 'replace_this_with_your_real_api';
$client = new APIClient($myAPIKey, 'http://api.wordnik.com/v4');
$wordApi = new WordApi($client);
$example = $wordApi->getTopExample('irony');
print $example->text;
?>
What does $currentDir return? Have you tried using __DIR__ or dirname(__FILE__) (they are the same) instead of that substr?
If you are using the second example just as you posted (without declaring $currentDir), so that is the problem at your original code: $currentDir is not returning the right folder!
With the original code, which file is being included? Because, actually, I think no one is! Use echo inside those if statements to check that!
The original function doesn't work because it is looking for the position of a UNIX-style forward slash '/', whereas you are on Windows and have backslashes '\'. That's a bug! I'll fix the lib to use dirname(FILE) as you do. Thanks for pointing out the error.
I am using this:
echo dirname(__FILE__);
which gives:
C:\UwAmp\www\myfolder\admin
However I am looking for path until:
C:\UwAmp\www\myfolder\
from current script. How can that be done ?
You could do either:
dirname(__DIR__);
Or:
__DIR__ . '/..';
...but in a web server environment you will probably find that you are already working from current file's working directory, so you can probably just use:
'../'
...to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.
You should also be aware what __DIR__ and __FILE__ refers to:
The full path and filename of the file. If used inside an include, the name of the included file is returned.
So it may not always point to where you want it to.
You can try
echo realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
echo dirname(__DIR__);
But note the __DIR__ constant was added in PHP 5.3.0.
Also you can use
dirname(__DIR__, $level)
for access any folding level without traversing
The parent directory of an included file would be
dirname(getcwd())
e.g. the file is
/var/www/html/folder/inc/file.inc.php
which is included in
/var/www/html/folder/index.php
then by calling /file/index.php
getcwd() is /var/www/html/folder
__DIR__ is /var/www/html/folder/inc
so dirname(__DIR__) is /var/www/html/folder
but what we want is /var/www/html which is dirname(getcwd())
To Whom, deailing with share hosting environment and still chance to have Current PHP less than 7.0 Who does not have dirname( __FILE__, 2 ); it is possible to use following.
function dirname_safe($path, $level = 0){
$dir = explode(DIRECTORY_SEPARATOR, $path);
$level = $level * -1;
if($level == 0) $level = count($dir);
array_splice($dir, $level);
return implode($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
print_r(dirname_safe(__DIR__, 2));
Use $_SERVER['DOCUMENT_ROOT']
For example if you want to access a folder above the root directory from anywhere
$_SERVER['DOCUMENT_ROOT']."/../myfolder/spl-auto.php"
or for your example C:\UwAmp\www\myfolder\admin
$admin = $_SERVER['DOCUMENT_ROOT']."/myfolder/admin/"
$myfolder = $_SERVER['DOCUMENT_ROOT']."/myfolder/"
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");