How to Get Filename from a Variable (Url) in PHP

How to Get filename from a variable (url) in php?

Use basename() function in php to return a file name from the path.Use the code below

<?php

$fileurl = 'http://example.com/filepath/filepath2/myfile.doc';
$file_name=basename($fileurl);
echo $file_name; // Will output myfile.doc

?>

Hope this helps you

Get current filename with URL parameters

You can append the query string after you get it as an environment variable:

$myurl = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];

If you need to consider the possibility of no query string, add a conditional test:

$myurl = strlen($_SERVER['QUERY_STRING']) ? basename($_SERVER['PHP_SELF'])."?".$_SERVER['QUERY_STRING'] : basename($_SERVER['PHP_SELF']);

Get only filename from url in php without any variable values which exist in the url

This should work:

echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);

But beware of any malicious parts in your URL.

Get filename and variable name from url in php which exist in the url

Try this if you want get file name only.

print_r(pathinfo("/Your url go here",PATHINFO_BASENAME));

The out will get file name with extension.

How to get file name from url without $_GET variable?

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.

Get file name of a URL in PHP

<?php
header('Content-Type: text/plain');

$curl = curl_init('http://localhost/fakefile.php');

curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');

if (($response = curl_exec($curl)) !== false)
{
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
{
var_dump($response);

$reDispo = '/^Content-Disposition: .*?filename=(?<f>[^\s]+|\x22[^\x22]+\x22)\x3B?.*$/m';
if (preg_match($reDispo, $response, $mDispo))
{
$filename = trim($mDispo['f'],' ";');

echo "Filename Found: $filename";
}
}
}

curl_close($curl);

That would parse the Content-Disposition line for the filename=foo.bar information (assuming it does render the file directly out using this method.)

get file name for url php

I think this is what you want:

<?php

$page = basename($_SERVER['PHP_SELF']);
?>

<div id="language">
<a href="../../<?php echo $page;?>" class="lang">EN</a>
<a class="lang" href="<?php echo $page;?>">DE</a>
<a class="lang" href="../pl/<?php echo $page;?>">PL</a>
</div>

You would save the current file name in the $page variable. Then echo it out and replace index.php in your links.

How do I get a file name from a full path with PHP?

You're looking for basename.

The example from the PHP manual:

<?php
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>


Related Topics



Leave a reply



Submit