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.
Trying to get a full URL without filename in PHP
There won't be one because it's not a useful attribute to track. If the current script is something other than index.*
then it will not return the correct URL for the current script.
However, this might get you what you want:
echo '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
Edit:
Also, aside from installing the php.net online manual search tool for Firefox and setting it up with a search keyword (which will let you pull up the documentation on the $_SERVER
global by typing something like "php $_server
" into your URL bar), you should also know that var_dump()
and print_r()
lets you output the full contents of any variable, including arrays and objects. This is very useful for debugging.
PHP: echo only the last part of a url, intead code printing the whole url
Try this..
<?php
$data = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$whatIWant = explode("/",$data);
echo end($whatIWant);?>
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.
PHP, get file name without file extension
No need for all that. Check out pathinfo(), it gives you all the components of your path.
Example from the manual:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // filename is only since PHP 5.2.0
Output of the code:
/www/htdocs
index.html
html
index
And alternatively you can get only certain parts like:
echo pathinfo('/www/htdocs/index.html', PATHINFO_EXTENSION); // outputs html
Get the full URL in PHP
Have a look at $_SERVER['REQUEST_URI']
, i.e.
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
(Note that the double quoted string syntax is perfectly correct)
If you want to support both HTTP and HTTPS, you can use
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.
How to get a file's extension in PHP?
People from other scripting languages always think theirs is better because they have a built-in function to do that and not PHP (I am looking at Pythonistas right now :-)).
In fact, it does exist, but few people know it. Meet pathinfo()
:
$ext = pathinfo($filename, PATHINFO_EXTENSION);
This is fast and built-in. pathinfo()
can give you other information, such as canonical path, depending on the constant you pass to it.
Remember that if you want to be able to deal with non ASCII characters, you need to set the locale first. E.G:
setlocale(LC_ALL,'en_US.UTF-8');
Also, note this doesn't take into consideration the file content or mime-type, you only get the extension. But it's what you asked for.
Lastly, note that this works only for a file path, not a URL resources path, which is covered using PARSE_URL.
Enjoy
Related Topics
How to Check If a Value Already Exists to Avoid Duplicates
JSON_Encode Not Working with a HTML String as Value
How to Eliminate PHP5 Strict Standards Errors
Detect Mobile Devices - and Tablet Devices
How to Install Imagemagick to Use with PHP on Windows 7 (3)
How to Check If a MySQL Query Using the Legacy API Was Successful
Extending Sonata User Bundle and Adding New Fields
Change the PHP Path to Mamps PHP
Using Prepared Statement, How I Return the Id of the Inserted Row
How to Print Multidimensional Arrays in PHP
How to Redefine a Type Hint to a Descendant Class When Extending an Abstract Class
Root Path Doesn't Work with PHP Include
How to Keep Form Values After Post
PHP Jquery Ajax Call Throws Net::Err_Empty_Response