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

Hide

How to send a postal cheque to lloyds bank plc

Why No One Should Ever Verify Their Details on Any Website

How to unblock a plug hole in your bath

Council tax letter template after receiving a summons

Html apostrophe single quote quote mark

Twitter friends diet tips

Visited link to remain same colour

Weird fish salmon pink hoody size l it is a bit bobbly otherwise good condition

Acts are not laws

How to make a million pounds

Why are the vaccinated not actually vaccinated

Why do people actually sell on ebay

How do I block someone on facebook

Natural Remedies and Dietary Approach for Psoriasis

How do I unblock someone on facebook

Does eating eggs increase your risk of diabetes

How to return only part of a feild from database using php substr but to take into account spaces

Are you a commercial electrical company

How to cure earache quickly and easily

How to stop bank scammers over the phone quickly and easily

Back magic instructions

The Hidden Truth That Ends All Loneliness Forever

What is the difference between Jam and peanut butter Joke on how i met your mother

Join the silent protest

Removing hairs from your ears using depilatory creams that are available in the market.

How to get your child into rainbows brownies guiding scouts beavers when there is a waiting list

How to get scammers off the phone and have some fun

Boost your business Promote your site here for free

Foods to avoid during H pylori infection

How to teach your child to swim easy lessons anyone can do

Should I take the covid Vaccine

How to remove item specifics on ebay

How to remove inspyware from your laptop

Potty Train your baby from 6 months old

Potty Train your baby from 6 months old

How do i edit a facebook album name in a group

Vodka Christmas cake recipe

How to stop people stealing your downloads when using paypal

Share







SubmitExpress.com