Welcome, Guest: Join CodeXpress Log In!

Stats: 12 Members, 112 topics and 9 posts Date:  Thursday 24 June 2021 at 10:14 AM

This is a product of CodeXpress Buy License for your website

Get the current script file name using PHP

CodeXpress / Science/Technology / PHP 7x / Get the current script file name using PHP

59 people viewed

Get The Current Script File Name Using Php
(Go Down) (» Reply topic)
Get the current script file name using PHP by Marshallunduemi(): Tue 07, July, 2020
If I have PHP script, how can I get the filename from inside that script?

Also, given the name of a script of the form jquery.js.php, how can I extract just the "jquery.js" part?


Just use the PHP magic constant __FILE__ to get the current filename.

But it seems you want the part without .php. So...

basename(__FILE__, '.php');

A more generic file extension remover would look like this...

function chopExtension($filename) {
return pathinfo($filename, PATHINFO_FILENAME);

var_dump(chopExtension('bob.php')); // string(3) "bob"
var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"

Using standard string library functions is much quicker, as you'd expect.

function chopExtension($filename) {
return substr($filename, 0, strrpos($filename, '.'));

When you want your include to know what file it is in (ie. what script name was actually requested), use:

basename($_SERVER["SCRIPT_FILENAME"], '.php')

Because when you are writing to a file you usually know its name.

As noted by Alec Teal, if you use symlinks it will show the symlink name instead.

Welcome to CodeXpress Forum Script - Download NL script Free
(» Go Up)

You must Sign in or Sign Up to post content

For you to post content on this website you have to log in or register, it only take few minute to create account

Go Up

Recommended for you

Get The Current Script File Name Using Php

Currently: 1 guest(s) reading this article

Developed by Codexpress Programming Labs
Post content
Powered by Marshall