How to return only the filename from $_SERVER['PHP_SELF'] in php

Return only the filename from $_SERVER['PHP_SELF']

Do you want to return only the filename of the page on the server without the directory listing as well.

1. First we assign the variable $self which uses $_SERVER['PHP_SELF'] to return the filename with the directory

$self = $_SERVER['PHP_SELF'];

2. Next we remove everything before the last instance of “/” as everything after this is our file name, everything before is our directory. We are using strrchr which takes everything in $self from the last position of “’/”.

$str2use = strrchr($self,'/');

3. Now we find the length of the string and minus 1 from this to remove the “/”.

$length = strlen($str2use)-1;

4. Lastly we use substr to return part of the string, we know the string starts with “/” so we want everything after this so we set the start vaule as 1, as 0 is where “/” is located and use the length of the string we already have. Hey presto you are just returning the filename now.

@$fname = substr($str2use, 1, $length);

The @ sign is used incase $_SERVER['PHP_SELF']; does not return a directory only the filename, ensuring that it will still work regardless.

Here is the code, with variables and also below is the code without the variables just the functions.

<?PHP

$self = $_SERVER['PHP_SELF'];

$str2use = strrchr($self,'/');

$length = strlen($str2use)-1;

@$fname = substr($str2use, 1, $length);

echo "$fname";

?>

<?PHP

@$fname=substr(strrchr($_SERVER['PHP_SELF'],'/'), -(strlen(strrchr($_SERVER['PHP_SELF'],'/')) -1));

?>

 


Trending right now

How to get out of paying ULEZ or similar charges

How to stop a form submitting when you press enter php

How to send a postal cheque to lloyds bank plc

How do i edit a facebook album name in a group

The power of the mind

Comparing vaccines to coke

How to make money from your website by doing very little

How to make a drop down box submit a form

Does Salt and Water Cure Hayfever

Contrails or chemtrails

How to find my saved buttons on paypal

How to stretch an image in a td cell to cover the whole cell using css

Using the power of fear to bring about a euromillions win

Get rich quick ideas

Visited link to remain same colour

The Quickest Ever Salt Dough Recipe

Correlation doesnt equal causation

How to remove item specifics on ebay

A Beginners Guide to Fitness for Women Over 50

Quantum Command Phrases and Reality Restructuring

Elegant Input box css

Enable the hidden admin on windows 10

Am I an antivaxer

How to create a date array in php which come up with todays date selected

Google ads for electricians in the UK

Good businesses to start with no experience

Peanut allergy and what to do in an emergency

How to make sure facebook picks up the correct picture from a website

What do you need help with in your business

Best feeling in the world

How to make first letter only captial using php.

Keep flies off your food this summer

Why Letting Go is the Key to Scaling Your Business

What to say when someone asks where is your mask

How to stop people stealing your downloads when using paypal

How do I block someone on facebook

Create a file to upload pictures or files in php

How to stop google chrome downloading automatically

How to make your laptop battery last longer

Share







SubmitExpress.com