I need get file folder name from url
examples:
http://domain/folder/NEEDTHIS/filename.xml
http://domain/folder/folder2/NEEDTHIS/filename.xml
http://domain/folder/folder2/folderanother/NEEDTHIS/filename.xml
I need only "NEEDTHIS" folder name only
i using this code for get file name
$parts = parse_url("http://domain/folder/NEEDTHIS/filename.xml");
$title = basename($parts['path']);
echo $title;
// Output: filename.xml
How can i get this file name folder?
With this approach you can even avoid parse_url()...:
$url = "http://domain/folder/NEEDTHIS/filename.xml";
$items = explode('/', $url);
echo $items[sizeof($items) - 2];
// Output: NEEDTHIS
Note: this solution only assumes directories/filenames are separated by a character. To be even more general, you could use DIRECTORY_SEPARATOR:
$url = "http://domain/folder/NEEDTHIS/filename.xml";
$items = explode(DIRECTORY_SEPARATOR, $url);
echo $items[sizeof($items) - 2];
Here you go. Use dirname() to get simple one-liner that works both for URL and normal dir-paths. Works too, if no file is specified in path (or when path ends at dir).
$path = 'http://example.com/root/one/two/three/four.xml';
echo end(explode('/',dirname($path)));
Outputs:
three
You can replace $path with anything.
Try this:
$url = "http://domain/folder/folder2/folderanother/NEEDTHIS/filename.xml";
$array = explode('/',$url);
$count = count($array);
echo $array[$count-2];
Related
I want to change filename in directory carbrands/alto/alto.php .Instead of alto.php I want to change as alto_new.php. But if I try to change name as
rename($old_name,$file_name);
After using this the filename changed but its not replace inside directory carbrands/alto instead its replaced out of directory. How to fix this issue?
rename("carbrands/alto/alto.php", "carbrands/alto/alto_new.php");
try this
I missed full path for $filename.Now I used full path for old and filename Now its worked correcly.
$pagename="carbrands/alto/alto.php";
$filename="alto_new.php";
$arr = explode("/", $page_name, 2);
$first = $arr[0];
$second1 = explode("/", $arr[1], 2);
$second = $second1[0];
$third = $second1[1];
$directory="$first/$second/";
foreach(glob('*.php') as $path_to_file) {
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($page_name,$file_name,$file_contents);
file_put_contents($path_to_file,$file_contents);
}
rename($directory.$third,$directory.$file_name);
You need to mention the entire path.
$old_name = 'carbrands/alto/alto.php';
$file_name = 'carbrands/alto/alto_new.php';
rename($old_name,$file_name);
http://localhost/mc/site-01-up/index.php?c=lorem-ipsum
$address = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$stack = explode('/', $_SERVER["REQUEST_URI"]);
$file = array_pop($stack);
echo $file;
result - index.php?c=lorem-ipsum
How to get just file name (index.php) without $_GET variable, using array_pop if possible?
Another method that can get the filename is by using parse_url — Parses a URL and return its components
<?php
$url = "http://localhost/mc/site-01-up/index.php?c=lorem-ipsum";
$data = parse_url($url);
$array = explode("/",$data['path']);
$filename = $array[count($array)-1];
var_dump($filename);
Result
index.php
EDIT:
Sorry for posting this answer as it is almost identical to the selected one. I didnt see the answer so posted. But I cannot delete this as it is seen as a bad practice by moderators.
I will follow parse_url() like below (easy to understand):-
<?php
$url = 'http://localhost/mc/site-01-up/index.php?c=lorem-ipsum';
$url= parse_url($url);
print_r($url); // to check what parse_url() will outputs
$url_path = explode('/',$url['path']); // explode the path part
$file_name = $url_path[count($url_path)-1]; // get last index value which is your desired result
echo $file_name;
?>
Output:- https://eval.in/606839
Note:- tested with your given URL. Check for other type of URL's at your end. thanks.
Try this, not tested:
$file = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $file);
$file = $parts[count($parts) - 1];
echo $file;
One way of doing it would be to simply get the basename() of the file and then strip-out all the Query Part using regex or better still simply do pass the $_SERVER['PHP_SELF'] result to the basename() Function. Both will yield the same result though the 2nd approach seems a little more intuitive.
<?php
$fileName = preg_replace("#\?.*$#", "", basename("http://localhost/mc/site-01-up/index.php?c=lorem-ipsum"));
echo $fileName; // DISPLAYS: index.php
// OR SHORTER AND SIMPLER:
$fileName = basename($_SERVER['PHP_SELF']);
echo $fileName; // DISPLAYS: index.php
If you are trying to use the GET method without variable name, another option would be using the $_SERVER["QUERY_STRING"]
http://something.com/index.php?=somestring
$_SERVER["QUERY_STRING"] would return "somestring"
I am in a trouble that how i can extract folder name from url
Example - http://localhost/posts/author/FOLDER_NAME/live/?red=home
I want to echo FOLDER_NAME to my webpage
<?php echo $name; ?>
There are several ways to do what you need, a simple one would be using preg_replace:
$url = "http://localhost/posts/author/FOLDER_NAME/live/?red=home"
$folder = preg_replace('%.*author/(.*?)/live.*%', '$1', $url);
echo $folder ;
Output:
FOLDER_NAME
Update based on your comment:
$url = "http://localhost/posts/author/FOLDER_NAME?red=home";
$folder = preg_replace('%.*author/(.*?)\?.*%', '$1', $url);
echo $folder ;
Output:
FOLDER_NAME
You can explode the string by "/" and get the correct element like this:
print explode("/", "http://localhost/posts/author/FOLDER_NAME/live/?red=home")[5];
I am trying to do a URL parser for my project, something very simple that gets an URL like you would enter it in a browser and convert it into a valid URL, once that is done parse such URL and grab all the images with full paths.
I was able to "fix" the user entered URL and pre-pend the http when needed, remove the last / on domain only (`http://www.domain.com/ become http://www.domain.com but http://www.domain.com/test/ stays unchanged).
The problem that I am having is dirname is parsing the path of certain folders.
my code looks something like this:
<?php
$url = 'www.domain.com/~folder/'; //This is a variable that changes often
$url = $this->fix_url($url); //$url is now http://www.domain.com/~folder/
$url_image = 'image.png';
$parse = parse_url($url);
$dir = (isset($parse['path'])?dirname($parse['path']):'');
$ret = 'http://'.$parse['host'].$dir.'/'.$url_image;
var_dump($parse, $dir, $ret);
?>
The way I was able to go around the problem is with this code that I use to find $dir
<?php
$path = (isset($parse['path'])?$parse['path']:'/');
$tmp = explode('/', $path);
if(is_array($tmp) && count($tmp) > 0){
array_pop($tmp);
}
$dir = implode('/', $tmp);
?>
But there must be a better way
I would like get the last path segment in a URL:
http://blabla/bla/wce/news.php or
http://blabla/blablabla/dut2a/news.php
For example, in these two URLs, I want to get the path segment: 'wce', and 'dut2a'.
I tried to use $_SERVER['REQUEST_URI'], but I get the whole URL path.
Try:
$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
Assuming $tokens has at least 2 elements.
Try this:
function getLastPathSegment($url) {
$path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
$pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
$pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash
if (substr($path, -1) !== '/') {
array_pop($pathTokens);
}
return end($pathTokens); // get the last segment
}
echo getLastPathSegment($_SERVER['REQUEST_URI']);
I've also tested it with a few URLs from the comments. I'm going to have to assume that all paths end with a slash, because I can not identify if /bob is a directory or a file. This will assume it is a file unless it has a trailing slash too.
echo getLastPathSegment('http://server.com/bla/wce/news.php'); // wce
echo getLastPathSegment('http://server.com/bla/wce/'); // wce
echo getLastPathSegment('http://server.com/bla/wce'); // bla
it is easy
<?php
echo basename(dirname($url)); // if your url/path includes a file
echo basename($url); // if your url/path does not include a file
?>
basename will return the trailing trailing name component of path
dirname will return the parent directory's path
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/function.basename.php
Try this:
$parts = explode('/', 'your_url_here');
$last = end($parts);
$arr = explode("/", $uri);
Another solution:
$last_slash = strrpos('/', $url);
$last = substr($url, $last_slash);
1: getting the last slash position
2: getting the substring between the last slash and the end of string
Look here: TEST
If you want to process an absolute URL, then you can use parse_url() (it doesn't work with relative urls).
$url = 'http://aplicaciones.org/wp-content/uploads/2011/09/skypevideo-500x361.jpg?arg=value#anchor';
print_r(parse_url($url));
$url_path = parse_url($url, PHP_URL_PATH);
$parts = explode('/', $url_path);
$last = end($parts);
echo $last;
Full code example here: http://codepad.org/klqk5o29
I wrote myself a little function to get the last dir/folder of an url. It only works with real/existing urls, not theoretical ones. In my case, that was always the case, so ...
function uf_getLastDir($sUrl)
{
$sPath = parse_url($sUrl, PHP_URL_PATH); // parse URL and return only path component
$aPath = explode('/', trim($sPath, '/')); // remove surrounding "/" and return parts into array
end($aPath); // last element of array
if (is_dir($sPath)) // if path points to dir
return current($aPath); // return last element of array
if (is_file($sPath)) // if path points to file
return prev($aPath); // return second to last element of array
return false; // or return false
}
Works for me! Enjoy! And kudos to the previous answers!!!
This will keep the part after the last slash.
No worries about explode, when for example no slash is there.
$url = 'http://blabla/blablabla/dut2a/news.php';
$url = preg_replace('~.*/~', '', $url);
Will give
news.php