Get The Current Script File Name Using PHP

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?

Answers

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.

0
0
166
Welcome to CodeXpress Forum Script - Download NL script Free


No comments. Be first to post a comment.

Reply to this article


Welcome, Guest: Join to reply login to reply!

♣ Post Statistic

1 guest(s) viewing this topic

Related Post

Get The Current Script File Name Using Php

♣ CodeXpress Statistic

Welcome, Guest: Join CodeXpress Login

17 Members, 135 topics and 9 posts
Date: Friday 01 July 2022 at 02:56 AM

Top 5 Contributors

People who started the most discussions on codexpress.

  1. 17
  2. 92
  3. 3
  4. 11
  5. 1

Sections

CodeXpress / General:
Politics, Business, Investment, Career, Autos, Counties, Properties, Agriculture, Health, Travel, Family, Culture, Religion, Food, Pets, Romance, Diaspora, Africa, International Forum, World List Directory, How To,
Science/Technology:
Programing, Webmasters, Computers, Phones, Art, Graphics & Video, Technology Market, CSS, Innovation, PHP 7x, Vanilla JS, Software Updates,

♣ Users Online: (1)

0 Members and 1 Guests online in last 5 minutes!


Sections:

Family, Travel, Culture, Health, World List Directory, PHP 7x, Technology Market, Software Updates, Programing, CSS, Forum Games, Jokes Etc, Fashion, Gaming, Music/Radio,

CodeXpress - Copyright © 2019 - 2022 Marshall Unduemi. All rights reserved. See How To Advertise.
Disclaimer: Every CodeXpress member is solely responsible for anything that he/she posts or uploads on CodeXpress.