?
Solved

PHP file_get_contents  How to get $self

Posted on 2009-07-10
5
Medium Priority
?
1,720 Views
Last Modified: 2012-08-14
I want to execute this script and use the file  it is located on as the  "a.php".

I have tried for a few hours to do this.

Instead of
$str = file_get_contents("a.php");

I need it to get something like this.
$str = file_get_contents("$self");

I am guessing.

Get the contents of it's own self.
It need just  run the code on it's self regardless of what the name of it's self is.

Right now it gets the info from "a.php" and creates a copy of "a.php" with a variable name.
I just nees it to get it's self and create a copy of it's self.

Apologies For repeating myself.  I just wanted to be clear.




Regards and Thank you....

SElvol
<?php
$str = file_get_contents("a.php");
        preg_match('%<meta name=\"keywords\" content=\"([^"]*)"%i',$str,$matchArray);
        $namesArray = explode(",",$matchArray[1]);
        $file = fopen($namesArray[0].$namesArray[1].$namesArray[3].".txt","w+");
        fwrite($file,$str);
        fclose($file);
?>

Open in new window

0
Comment
Question by:selvol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:Gawai
ID: 24821551
try

<?php
$self = $_SERVER['PHP_SELF']
$str = file_get_contents($self);
        preg_match('%<meta name=\"keywords\" content=\"([^"]*)"%i',$str,$matchArray);
        $namesArray = explode(",",$matchArray[1]);
        $file = fopen($namesArray[0].$namesArray[1].$namesArray[3].".txt","w+");
        fwrite($file,$str);
        fclose($file);
?>
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24821580
Try using the pre-defined constant  __FILE__ :

$str = file_get_contents(__FILE__);

...or use PHP_SELF in the $_SERVER superglobal array:

$str = file_get_contents($_SERVER['PHP_SELF']);

...or use SCRIPT_NAME, also from the $_SERVER array:

$str = file_get_contents($_SERVER['SCRIPT_NAME']);
0
 
LVL 17

Author Comment

by:selvol
ID: 24821966
I am getting several errors on every version of the script.
I know it's most likely MY fault. As I a fairly New to PHP.
Possibly I explained it incorrectly..
Attached is a file with two PHP files in it.
Run the Rename.php and see how it gets a.php and copies it.  to SMITHJOHNPLAIN.txt
I would like to do the same thing but with out a.php involved.
Rename the file to .rar
Thank you

renametoRAR.txt
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 2000 total points
ID: 24822133
I changed rename.php like this:

$str = file_get_contents(__FILE__);

...and it produces a file named SMITHJOHNPLAIN.txt, containing a copy of the rename.php script.

Is this what you are trying to do?
0
 
LVL 17

Author Comment

by:selvol
ID: 24822225
CXR,
Yes, I must have done some thing to cause an error...
 $str = file_get_contents(__FILE__);   ---------- PERFECT!
I had tried that in the hours I attempted to solve this myself. Must have been off.
 
$self = $_SERVER['PHP_SELF']   did not work.  Not sure why. Maybe my Fault.

I Appreciate you both for the help.
SelVOl
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question