I am running this code to read a directory (on Apache server):
$mydir = '/media/video/';
$root_dir = $_SERVER["DOCUMENT_ROOT"];
if(strpos($_SERVER['HTTP_HOST'], 'localhost') === false){
$dir = $root_dir . $mydir;
}else{
$dir = $mydir;
}
There have been some server configuration changes recently and now it returns this:
/home2/interact/public_html
Is there a rmore reliable way to always get corrent root path?
I need a public_html path.
// split url path on '/' slash characher and save result in $urlParts(array). check if $urlParts array contains public_html. it means /home2/interact/public_html is root.
$dirPath = dirname ( FILE );
$urlParts = explode('/', $dirPath);
if(in_array('public_html', $urlParts)) {
//do something
}
Related
I have this short script to detect the user usage environment and try to normalize the root path:
Located in C:\xampp\htdocs\dev\t2\Last-Hammer\configs\const\loader.php
$env= '';
if (php_sapi_name() == 'cli') {
$env= 'cli';
if (!isset($_SERVER['PWD'])) {
$path = dirname(__DIR__).'\\';
} else {
$path = dirname($_SERVER['PWD']);
}
} else {
$env= 'web';
$path = $_SERVER['DOCUMENT_ROOT'];
}
echo $path;
echo PHP_EOL;
echo $env;
file_get_contents($path.'configs/const/client.xml')
i use it from 2 diferent files: index.php that wor well in root folder but trying to use it from a sub-directory like this /dev/cron.php
cron.php content:
$path = (!isset($_SERVER["PWD"]) ? dirname(__DIR__).'\\' : dirname($_SERVER["PWD"]));
require_once $path.'/configs/const/loader.php';
i get this output
//from Web environment
C:/xampp/htdocs/dev/t2/Last-Hammer/
web
and
//from CLI environment
C:\xampp\htdocs\dev\t2\Last-Hammer
cli
the problem is that this work correctly from Web environment but not work correctly in CLI, when i try to execute like: php cron.php code try to make a file_get_contents... like this using cli get this error:
PHP Warning: file_get_contents(C:\xampp\htdocs\dev\t2\Last-Hammer\configs\configs/const/client.xml): failed to open stream: No such file or directory in C:\xampp\htdocs\dev\t2\Last-Hammer\configs\const\loader.php on line 24
Warning: file_get_contents(C:\xampp\htdocs\dev\t2\Last-Hammer\configs\configs/const/client.xml): failed to open stream: No such file or directory in C:\xampp\htdocs\dev\t2\Last-Hammer\configs\const\loader.php on line 24
what is expected is that both for CLI or WEB, the root of the project is similar to: C:/xampp/htdocs/dev/t2/Last-Hammer/ and does not change constantly in the case of CLI depending on where it is executed the php file, as root could set in CLI. regardless of where it runs.
You are lacking trailing "\" on cli case. to uniform both, you could use str_replace():
$env= '';
if (php_sapi_name() == 'cli') {
$env= 'cli';
if (!isset($_SERVER['PWD'])) {
$path = dirname(__DIR__).'\\';
} else {
$path = dirname($_SERVER['PWD']).'\\';
}
} else {
$env= 'web';
$path = $_SERVER['DOCUMENT_ROOT'];
}
$path = str_replace( '\\', '/', $path );
echo $path;
echo PHP_EOL;
echo $env;
file_get_contents($path.'configs/const/client.xml')
define in your main scripts:
define('DOCROOT', '../'); // this in your cron.php
define('DOCROOT', './'); // this in your index.php
Just use relative paths:
$path = DOCROOT;
file_get_contents($path.'configs/const/client.xml')
<?php
if (isset($_POST['filename']) && isset($_POST['editorpassword']) && isset($_POST['roomname'])) {
$dir = $_POST['filename']; // This must match the "name" of your input
$path = "evo/" . $dir;
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
}
?>
I have this script where I'm trying to create a new folder. The script itself is ran inside of a folder called /evo and by using this code, it creates the folder in there. Where it needs to go is ../../creative however even if I try and use
$path = "./rooms/creative/" . $dir;
or something to that effect it creates it with the base folder as evo so it appears at:
../evo/rooms/creative (creating the folders that don't exist there with it as it should)
I'm just unsure what to write in for the path on where I need it created to find the right location.
Simplest solution is to remove the "evo" in $path = "evo/" . $dir;
My filemanager folder is located here:
localhost/cakeapp/app/webroot/js/tinymce/js/tinymce/plugins/filemanager/
In cakeapp root directory, I have two folders:
uploadedImages (cakeapp/uploadedImages)
UploadedImagesThumbs (cakeapp/uploadedImagesThumbs).
I have this situation in config.php File:
$base_url ="http://localhost";
// path from base_url to base of upload folder (with start and final /)
$upload_dir = '/cakeapp/uploadedImages/';
// relative path from filemanager folder to thumbs folder (with final /)
$thumbs_base_path = '???????/uploadedImagesThumbs';
// relative path from filemanager folder to upload folder (with final /)
$current_path = '???????/uploadedImages/';
How I get the correct relative path for $thumbs_base_path and $current_path from filemanager folder?
You could try something like
function getRelativePath($from, $to) {
$patha = explode('/', $from);
$pathb = explode('/', $to);
$start_point = count(array_intersect($patha,$pathb));
while($start_point--) {
array_shift($patha);
array_shift($pathb);
}
$output = "";
if(($back_count = count($patha))) {
while($back_count--) {
$output .= "../";
}
} else {
$output .= './';
}
return $output . implode('/', $pathb);
}
Check the source.It has many more examples
I'm trying to turn the files in my 'objects' directory into an array, then use them to load the objects. But, for some reason, I continue to get this error
Warning: opendir(C:\xampp\htdocs/objects,C:\xampp\htdocs/objects): The system cannot find the file specified. (code: 2)
here is the code:
public function loadObjects(){
$files = array();
if ($handle = opendir(APP_PATH . 'objects'))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
$files[] = $entry;
}
}
}
closedir($handle);
if(is_array($files) && count($files) > 0)
{
foreach($files as $value)
{
require_once(APP_PATH . 'objects/' . $value);
$value = stristr($value, '.', true);
self::$objects[$value] = new $object(self::$instance);
}
}
}
I know this is an old question but for any future viewers I will post an anwser just in case.
This type of error usually comes from a simple oversight. When developing most aplication the developer usualy uses a path like
http://localhost/myAppHome
or
http://96.82.102.233/myAppHome(if on remote server)
In this perticular case the APP_PATH is probably defined somethig like that:
define('APP_PATH',$_SERVER['DOCUMENT_ROOT']);
This will be wrong in every case when the app is being developed outside of a domain name.
$_SERVER['DOCUMENT_ROOT'] will resolve to the root of domain which in this case will be
http://localhost or http://96.82.102.233
The main directory for localhost or the IP address is going to be the diretory root of the server itself => drive:/xampp/htdocs (for example)
Basically to avoid this issue you should always mind not to ask for 'DOCUMENT_ROOT' when developing without a domain pointing to you app.
If you dont require reqular deploys you can just add the missing folder to the definition like so :
define('APP_PATH',$_SERVER['DOCUMENT_ROOT'].'/myAppHome');
In case you deploy on reqular basis and you are afraid you will forget to rever this change before depoying you can always insert an IF when defing APP_PATH like:
if($_SERVER['SERVER_NAME']=='localhost'){
define('APP_PATH', $_SERVER['DOCUMENT_ROOT'].'/myAppHome');
}else{
define('APP_PATH', $_SERVER['DOCUMENT_ROOT']);
}
You are trying to open that directory with a "/".
Try to replace:
C:\xampp\htdocs/objects
to
C:\xampp\htdocs\objects
Please be sure APP_PATH variable is not null and correct values. There is no scandir function usage on your codes.
After that, i suggest you to use DirectoryIterator.
http://www.php.net/manual/en/class.directoryiterator.php
Complete example:
http://fabien.potencier.org/article/43/find-your-files
APP_HOST = DIR folder;
APP_PATH = APP_PATH + DIR folder;
Example = "C:/xampp/htdocs" + "/parent/child/index.php"
if ($_SERVER['SERVER_NAME'] == "localhost") {
define('APP_HOST', pathinfo($_SERVER['PHP_SELF'], PATHINFO_DIRNAME));
define('APP_PATH', $_SERVER['DOCUMENT_ROOT'] . APP_HOST);
} else {
define('APP_PATH', $_SERVER['DOCUMENT_ROOT']);
}
I have a script which displays the images present on a folder.
E.g. Script path (from HTDOCS):
/admin/global/thisscript.php
E.g. Pictures folder path (from HTDOCS):
/public/test/images/
Now I need to print under each image the path of the file starting from the HTDOCS directory.
E.g. if I have:
$picture1 = /WEB/mysite/htdocs/public/test/images/picture1.png
$picture1name = basename($picture1)
I'd like to print
print "/public/test/images/".$picture1name
having
/public/test/images
in a variable which is automatically update if picture1.png path changes.
If I use
$_SERVER['PHP_SELF']
it returns the path of the file I'm displaing. Also
__FILE__
__DIR__
basename
dirname
are not working for me.
How can I get the path?
Thank you
perhaps $_SERVER['DOCUMENT_ROOT']
edit:
Something like:
$path = "/WEB/mysite/htdocs/public/test/images/picture1.png";
$path = dirname(str_replace($_SERVER['DOCUMENT_ROOT'], '', $path));
Not sure if this could help but could you explode the path result you have and build it back from the HTDOCS entry? something like:
$fullpath = /WEB/mysite/htdocs/public/test/images/picture1.png
$pathbits = explode("/", $fullpath);
$newpath = "";
$foundstart = false;
foreach($pathbits as $key => $value){
if ($value == "htdocs" || $foundstart == true) {
$foundstart = true;
$newpath = $newpath + "/" + $value;
}
}