Link to home
Start Free TrialLog in
Avatar of James Williams
James WilliamsFlag for United States of America

asked on

PHP file_get_contents How to get $self

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

Avatar of Emad Gawai
Emad Gawai
Flag of United Arab Emirates image

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);
?>
ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James Williams

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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