Find FILE path relative to the htdocs directory - php

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

Related

How to detect folders names in a directory usinig php?

I need php to detect folder names in a directory on my computer not in the project directory in wamp server. i tried all the answers on
Using scandir() to find folders in a directory (PHP)
but it didn't work
it says the directory is not on the project directory when i tested scandir as in this code:
$path = 'extracted/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
//code to use if directory
}
}
I am trying this also but i got empty array
$path = 'D:\tests\Ed';
$folders=glob("{$path}/*");
print_r($folders);
the only way i could run the code is by putting the folder in the project directory ,
$path = 'C:\wamp\www\my project php\ED';
but is there another way?

PHP creating a folder with the right path

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

Get server root directory

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
}

Getting relative path from folder

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

Warning: opendir(): The system cannot find the file specified. (code: 2)

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']);
}

Categories