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

Back magic instructions

Good businesses to start with no experience

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

Potty Train your baby from 6 months old

Potty Train your baby from 6 months old

How to find my saved buttons on paypal

Natural Remedies and Dietary Approach for Psoriasis

How to unmark posting as spam on facebook

How to open a jar easily without strain

How to find your others messages in facebook links for mobile and web

What different Text Ads Types are available for Google ADWORDS

Vodka Christmas cake recipe

How to get children to eat more fruit

How to stop paying your Council Tax

Cease and desist letter to send to the bailiffs to stop them collecting council tax

The only cough remedy that really works

Clear your credit file of all debts

Join the silent protest

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

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

Ebay Scam buyer has said that parcel has not arrived

How to get my site listed in google in quickly

People Do not Really Care And That is the Truth

Boost your business Promote your site here for free

How to remove item specifics on ebay

Should I take the covid Vaccine

The Hidden Question That Tricks Your Brain Into Attracting Massive Wealth Effortlessly

The Hidden Question That Tricks Your Brain Into Attracting Massive Wealth Effortlessly

When to start potty training

Foods to avoid during H pylori infection

How does the covid vaccine protect others

Deregister letter for school for England. How to deregister your child from school

How to send a postal cheque to lloyds bank plc

The Hidden Truth That Ends All Loneliness Forever

Download free instructions for the game chad valley link letters

Disney frozen bath towel very good condition

Instructions for tell the time galt game

Best feeling in the world

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

Lowell debt collection agency. How to get them off your back.

Share







SubmitExpress.com