FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » PHP script to get name of file which houses the script?
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
PHP script to get name of file which houses the script? [message #184997] Sun, 23 February 2014 14:47 Go to next message
rnetzlof is currently offline  rnetzlof
Messages: 2
Registered: February 2014
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
I have a number of files which contain PHP scripts like:

<?php
$file_name='mtg10.php';
include 'format_date.php';
?>

The included PHP script uses $file_name to get the last uploaded time stamp of the named file and process it into a displayable line. All that works as intended.

What I don't like is the need for $file_name='mtg10.php'; It would be much handier if I could write something like $file_name=getNameOfThisFile(); instead.

I've looked, can't find any such function, but maybe I haven't looked in the right references. Is there such a function or a sequence of function calls to produce that effect?

Bob Netzlof
Re: PHP script to get name of file which houses the script? [message #184998 is a reply to message #184997] Sun, 23 February 2014 15:30 Go to previous messageGo to next message
Christian Stadler is currently offline  Christian Stadler
Messages: 2
Registered: February 2014
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
Am 23.02.2014 20:47, schrieb rnetzlof(at)gmail(dot)com:
> I have a number of files which contain PHP scripts like:
>
> <?php
> $file_name='mtg10.php';
> include 'format_date.php';
> ?>
>
> The included PHP script uses $file_name to get the last uploaded time stamp of the named file and process it into a displayable line. All that works as intended.
>
> What I don't like is the need for $file_name='mtg10.php'; It would be much handier if I could write something like $file_name=getNameOfThisFile(); instead.
>
> I've looked, can't find any such function, but maybe I haven't looked in the right references. Is there such a function or a sequence of function calls to produce that effect?

http://www.php.net/manual/en/language.constants.predefined.php should
answer your question.

--
Christian Stadler
Re: PHP script to get name of file which houses the script? [message #185001 is a reply to message #184997] Sun, 23 February 2014 15:34 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
rnetzlof(at)gmail(dot)com wrote:

> I have a number of files which contain PHP scripts like:
>
> <?php
> $file_name='mtg10.php';
> include 'format_date.php';
> ?>
>
> The included PHP script uses $file_name to get the last uploaded time
> stamp of the named file and process it into a displayable line. All
> that works as intended.
>
> What I don't like is the need for $file_name='mtg10.php'; It would be
> much handier if I could write something like
> $file_name=getNameOfThisFile(); instead.
>
> I've looked, can't find any such function, but maybe I haven't looked
> in the right references. Is there such a function or a sequence of
> function calls to produce that effect?

Well, there is no such function, AFAICT, but a "magic" constant:
__FILE__[1].

[1] <http://www.php.net/manual/en/language.constants.predefined.php>

--
Christoph M. Becker
Re: PHP script to get name of file which houses the script? [message #185002 is a reply to message #184997] Sun, 23 February 2014 15:40 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Sun, 23 Feb 2014 11:47:04 -0800, rnetzlof wrote:

> What I don't like is the need for $file_name='mtg10.php'; It would be
> much handier if I could write something like
> $file_name=getNameOfThisFile(); instead.

is this:

$file_name = __FILE__;

what you're looking for?

http://www.php.net/manual/en/language.constants.predefined.php

Make sure you read the manual page before using it, as it's quite likely
not going to give exactly the same value as "$file_name =
string_literal;" will.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: PHP script to get name of file which houses the script? [message #185007 is a reply to message #184997] Sun, 23 February 2014 16:00 Go to previous messageGo to next message
Robert Heller is currently offline  Robert Heller
Messages: 60
Registered: December 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
At Sun, 23 Feb 2014 11:47:04 -0800 (PST) rnetzlof(at)gmail(dot)com wrote:

>
> I have a number of files which contain PHP scripts like:
>
> <?php
> $file_name='mtg10.php';
> include 'format_date.php';
> ?>

<?php
/* __FILE__ is the full pathname of the current file. basename() returns
* the tail.
*/
$file_name=basename(__FILE__);
include 'format_date.php';
?>

>
> The included PHP script uses $file_name to get the last uploaded time stamp of the named file and process it into a displayable line. All that works as intended.
>
> What I don't like is the need for $file_name='mtg10.php'; It would be much handier if I could write something like $file_name=getNameOfThisFile(); instead.
>
> I've looked, can't find any such function, but maybe I haven't looked in the right references. Is there such a function or a sequence of function calls to produce that effect?
>
> Bob Netzlof
>

--
Robert Heller -- 978-544-6933 / heller(at)deepsoft(dot)com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Re: PHP script to get name of file which houses the script? [message #185011 is a reply to message #184997] Sun, 23 February 2014 16:57 Go to previous message
rnetzlof is currently offline  rnetzlof
Messages: 2
Registered: February 2014
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
On Sunday, February 23, 2014 2:47:04 PM UTC-5, rnet...@gmail.com wrote:
> I have a number of files which contain PHP scripts like:
>
> <?php
> $file_name='mtg10.php';
> include 'format_date.php';
> ?>

Yes $file_name=basename(__FILE__); does just what I wanted. Thanks to all who replied.

Bob Netzlof
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: Help with regex
Next Topic: JavaScript to PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Oct 18 00:04:03 EDT 2017

Total time taken to generate the page: 0.00701 seconds