Solved

PHP file_get_contents  How to get $self

Posted on 2009-07-10
5
1,695 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 500 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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

617 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