Closed Thread Icon

Preserved Topic: What's the PHP equivelant of DOCUMENT_URI? Pages that link to <a href="https://ozoneasylum.com/backlink?for=20951" title="Pages that link to Preserved Topic: What&amp;#039;s the PHP equivelant of DOCUMENT_URI?" rel="nofollow" >Preserved Topic: What&#039;s the PHP equivelant of DOCUMENT_URI?\

 
Author Thread
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-15-2001 06:39

I'm trying to determine the file name of a .php file from within that same php file (determine it's own file name).

Is there a built in variable for that?



Tikita
Nervous Wreck (II) Inmate

From: Canada
Insane since: Jun 2001

posted posted 07-15-2001 07:33

$PHP_SELF

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 07-15-2001 11:26

$PHP_SELF (variable) - The filename of the currently executing script, relative to the document root. If PHP is running as a command-line processor, this variable is not available.

__FILE__ (case-insensitive, constant) - The name of the script file presently being parsed. If used within a file which has been included or required, then the name of the included file is given, and not the name of the parent file.

Example:

FileName: <?=basename($PHP_SELF)?>

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-15-2001 13:29

Oh, I hadn't read about basename() yet. That makes it easier. I was using:

$lastslash = substr(strrchr ($PHP_SELF, "/"), 1); // grab the filename
$myfilename = str_replace(".php",".pdf", $lastslash); // change the extension
$isitthere = file_exists($myfilename); // see if the new file exists
if ($isitthere){ // if it does....
$myfilesize = filesize($myfilename); // get the file size of the new file
$mykbsize = round($myfilesize / 1024); // convert the file size to kb and round it off
print("<font face=verdana size=1><a href=" . $myfilename . ">Download this release in Acrobat Reader format (" . $mykbsize . " kb)</a></font><br>\n"); // print the resulting link
}

But I see I could use $lastslash = basename($PHP_SELF); instead.

Thanks, as always, Mr. Max!

[edited by mr.maX]fixed function name in Pugzly's first sentence[/edit]

[This message has been edited by mr.maX (edited 07-15-2001).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 07-15-2001 14:22

I don't know how your concept for that web site looks like, but I would suggest you to put all common parts of code as functions in one big library which you will include at the top of each file. Also, you don't need to use so many variables. Here's more compact version of that code:

<?
// shows link to PDF file (if available)
function pdfLink() {
&nbsp;&nbsp;&nbsp;&nbsp;global $PHP_SELF;
&nbsp;&nbsp;&nbsp;&nbsp;$pdfFileName = str_replace(".php", ".pdf", basename($PHP_SELF));
&nbsp;&nbsp;&nbsp;&nbsp;if (file_exists($pdfFileName)) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$pdfKbSize = round(filesize($pdfFileName) / 1024);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo "<font face=\"verdana\" size=1><a href=\"$pdfFileName\">Download this release in Acrobat Reader format ($pdfKbSize kb)</a></font><br>\n";
&nbsp;&nbsp;&nbsp;&nbsp;}
}
?>

And you can call it very easy whereevr you need that link:

<? pdfLink(); ?>

BTW Besides basename() function, there's also dirname() function which returns path (in case that you need it in future)...

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-16-2001 05:40

Ahhh...ok. I was trying to combine the different commands together, but couldn't quite get it.

I'm quite proud of myself, actually, for getting it to work. I like your version, Mr. Max, because it shows my how to combine things together to shorten the code.

Not so sure I understand the "global $PHP_SELF;" part, but the rest makes perfect sense.

Thanks for help. I'm really starting to enjoy this PHP stuff. Two books down this weekened alone.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 07-16-2001 20:11

All variables that are defined outside functions aren't available inside them, unless you specify that they are global (and that's what "global $PHP_SELF;" does).

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-16-2001 22:10

Aha! Sooner or later I would have needed to know that.

BTW, Max - I LOVE how HTML Beauty does PHP code. Very slick!

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 07-16-2001 23:33

Hey Pugzly

Which books did you read? And, did you think either of them good enough to recommend?

- Resolutions, Of All My Fruitless Searches -

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-16-2001 23:59

I like this one

Takes a web centric application development approach to PHP and MySQL..

I've found I don't really need a refrence with PHP since it has such awesome online documentation


Walking the Earth like Kane

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-17-2001 05:46

The first one was PHP for the World Wide Web - A Visual Quickstart Guide. It's by Larry Ullman and put out by Peachpit Press. I read most of the 267 pages.

The other is Essential PHP for Web Professionals by Christopher Cosentino. It's published by Prentice Hall Computer Books. Both are pretty good. The second one dives into MySQL a lot more, including step by step on setting up databases and stuff. I've gone through most of that one, and I'm still trying some of the items in that book.

They are good beginners books (which is what I need), and I would recommend both.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 07-17-2001 14:46

Thanks Pugzly and Bitdamaged for the book recommendations. There are so many of them on the subject, that I never really know which ones are worth purchasing. I'm much more comfortable buying them after a thumbs up from you guys.



- Resolutions, Of All My Fruitless Searches -

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-17-2001 16:12

Yeah, PHP docs are pretty good. In fact, if you go to http://www.php.net, you can download a Windows Help file version of the PHP manual. I love that. It makes looking up things SO much easier.

« BackwardsOnwards »

Show Forum Drop Down Menu