Define absolute path for files - php

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

Related

php rename (move) from one dir to another that is two levels back

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.

Php copy() is not working properly

I'm trying to copy my uploaded file to another directory called img folder with following code. But it doesn't work properly. I don't know why ? can you help me plz ?
php Code:
if($image["name"] != "")
{
//$path = PATH . DS . "uploads" . DS . "products" . DS . $id;
$path = "../../uploads" . DS . "products" . DS . $id;
$path2 = "img";
if(!is_dir($path))
{
mkdir($path);
}
chmod($path, 0755);
//move_uploaded_file($image["tmp_name"], $path . DS . $image["name"]);
move_uploaded_file($image["tmp_name"], $path . DS .
$uploadImage);//exit;
copy($uploadImage, $path2);
}
Following error message show:
Warning: copy(249.jpg) [function.copy]: failed to open stream: No such file or directory in...
The copy() function needs the full file path for the source file; you're just passing the filename, not the path.
As things stand, it's looking in the current folder for the file, not finding it, and throwing the error as a result.
From the previous line of code, it looks like your full path should be $path . DS . $uploadImage, so the copy command should look like this:
copy($path . DS . $uploadImage, $path2);
hope that helps.
Your copy function is wrong...
copy($uploadImage, $path2);
As Spudley answer says, you have to use the full path of the image. Also, $path2 is a directory. You have to give a name for the new copy of the image.
So, your copy function would be as follows:
copy($path . DS . $uploadImage, $path2. DS . $uploadImage);
Try it and let us know.

Get folder up one level

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

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:

How define a unique "plugins" path to all apps in 1.3?

In 1.2 i used:
$pluginPaths = array(CAKE_CORE_INCLUDE_PATH . DS. 'vendors' . DS.
'plugins' . DS);
But.. in 1.3 we have the new plugins path, and i believe thats not
necessary the $pluginsPaths manual declaration if i have:
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
//define('CAKE_CORE_INCLUDE_PATH', ROOT);
define('CAKE_CORE_INCLUDE_PATH', $_SERVER['DOCUMENT_ROOT'] . DS .
"libs2");
}
I saved my plugin inside the "plugins path"...
But i have not sucess in:
var $components = 'Plugin.Component';
Anyone can help me?
Thanks, Celso.
Probably you need this.

Categories