Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1756
  • Last Modified:

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

0
selvol
Asked:
selvol
  • 2
  • 2
2 Solutions
 
GawaiCommented:
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
 
Roger BaklundCommented:
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
 
selvolAuthor Commented:
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
 
Roger BaklundCommented:
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
 
selvolAuthor Commented:
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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now