Solved

PHP file_get_contents  How to get $self

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How can I split the array as 4 rows per line? 5 62
Creating a slider 12 34
mysql left join sentence 7 22
sql sentence 2 12
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 is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now