PHP Get only directory path - php

I have:
$page_file_temp = $_SERVER["PHP_SELF"];
which will output: /templates/somename/index.php
I want to extract from that path only "/templates/somename/"
How can I do it?
Thanks!

$page_directory = dirname($page_file_temp);
See dirname.

Maybe this is your solution:
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$thisPath = dirname($_SERVER['PHP_SELF']);
$onlyPath = str_replace($rootPath, '', $thisPath);
For example:
$_SERVER['DOCUMENT_ROOT'] is the server's root-path like this /home/abc/domains/abc.com/public_html
$_SERVER['PHP_SELF'] is about the whole path to that script like this /home/abc/domains/abc.com/public_html/uploads/home/process.php
Then we can have:
$rootPath like this /home/abc/domains/abc.com/public_html
$thisPath like this /home/abc/domains/abc.com/public_html/uploads/home
And $onlyPath like this /uploads/home

Take a look at the dirname() function.
From the documents, dirname() removes the trailing slash. If you want to keep it you can append the constant DIRECTORY_SEPARATOR to the result.
$dir = dirname('mystring/and/path.txt').DIRECTORY_SEPARATOR;

Using parse_url will account for GET variables and "fragments" (portion of URL after #) amongst other URL-specific parts.
$url = $_SERVER['PHP_SELF']; // OR $_SERVER['REQUEST_URI']
echo parse_url($url, PHP_URL_PATH);

An alternative:
$directory = pathinfo($page_file_temp,PATHINFO_DIRNAME);
http://www.php.net/manual/en/function.pathinfo.php

Related

PHP: How to explode string

I have a variable that stores the location of a temp file:
$file = 'C:\xampp\htdocs\temp\filename.tmp';
How can I explode all this to get filename (without the path and extension)?
Thanks.
Is not the best code but if you confident that this path will be similar and just file name will be different you can use this code:
$str = 'C:\xampp\htdocs\temp\filename.tmp';
$arrayExplode = explode("\\", $str);
$file = $arrayExplode[count($arrayExplode)-1];
$filename = explode('.', $file);
$filename = $filename[0];
echo $filename;
Advice: Watch out on the path contain "n" like the first letter after the backslash. It could destroy your array.
You should use the basename function, it's meant specifically for that.

PHP: take a path from site root given an absolute path

Maybe a dumb question: given a full path like:
C:/wamp/www/acme/archivio/subfolder/C00005/FATCLI/2014-V00011.pdf
and given that site root is acme, how can I get the part:
/archivio/subfolder/C00005/FATCLI/2014-V00011.pdf
in the easiest way?
You can use explode -
$path = 'C:/wamp/www/acme/archivio/subfolder/C00005/FATCLI/2014-V00011.pdf';
$paths = explode('acme', $path);
echo $paths[1];
explode()
You can variable also -
$root = 'acme';
$paths = explode($root, $path);

Regex to convert directory paths in PHP

Goal -
convert a path: /aaaaa/bbbbb/ccccc/dddd
to a relative path (to the root): ../../../../
So far I've come up with this regex: /\/.+?\//
but this only produces: ..bbbbb..dddd because it is only matching every other pair of slashes, and also matching the slashes. I'm looking for something like a string split, but also replace.
All of my php code:
$pattern = '/\/.+?\//';
$path = '/aaaaa/bbbbb/ccccc/dddd';
echo preg_replace($pattern, '..', $path);
preg_replace('/\/{0,1}(\w+)\/{0,1}/', '../', $path);
This is working for me.
How about:
preg_replace(':/[^/]+:', '../', $path);
what about the below ?
$pattern = '/\//';
$path = '/aaaaa/bbbbb/ccccc/dddd';
preg_replace(array($pattern), array('..'), $path

wordpress extract media url

$path = parse_url($post->guid, PHP_URL_PATH);
echo "<pre>";
print_r($path);
echo "<br>";
here i get
/wp-content/uploads/2014/01/kl-2-256.png
/wp-content/uploads/2014/04/bg-eBook.pdf
here i want to remove /wp-conent/uploads from these paths and extract only year month and image name
i tried with
$segments = explode('/', rtrim($path, '/'));
but not working properly every time
is there any proper and best solution?
Would something like this work for you?
$path = parse_url($post->guid, PHP_URL_PATH);
$path = str_replace("wp-content/uploads/", "", $path);
echo $path;
Use the list() construct to map the three data you need. The code is exploding the path by / and then looks from behind and passes those values to your mapped variables of list.
$path = '/wp-content/uploads/2014/01/kl-2-256.png';
list($year,$month,$image)=array_slice(explode('/',$path),-3,3);
You can then print $year,$month and $image separately.

Removing part of path in php

I have a path like:
$path='somefolder/foo/bar/lastdir';
and I want to remove the last part, so I have:
$path='somefolder/foo/bar';
Like I went one folder up.
I'm really newbie in php, maybe its just one function, although I can't find it anywhere.
You could try this (tested and works as expected):
$path = 'somefolder/foo/haha/lastone';
$parts = explode('/', $path);
array_pop($parts);
$newpath = implode('/', $parts);
$newpath would now contain somefolder/foo/haha.
use :
dirname(dirname('somefolder/foo/haha/lastone/somescript.php'));
this should return:
somefolder/foo/haha/
This is untested, but try:
$path_array = explode('/',$path);
array_pop($path_array);
$path = implode('/',$path_array);
If you are currently at:
somefolder/foo/haha/lastone/somescript.php
and you want to access:
somefolder/foo/haha/someotherscript.php
just type:
../someotherscript.php
Probably using a regex function would be appropriate if the last part is going to vary. Try
$pattern = '#/.*$#U';
$stripped_path = preg_replace($pattern, '', $original_path);
This will strip everything off the original path string starting from the last forward slash.
You could use a function that explodes() the $path variable into an array and then array_pop to get rid of the last element.
function path($path) {
$arrayPath = explode("/", $path);
$path = array_pop($arrayPath);
return $path = implode("/", $path);
}
The shortest variant in PHP is:
$path = preg_replace('|/[^/]*$|','', $path);
which uses a regular expression.

Categories