简体   繁体   中英

Get current page name with file created by a PHP file

So I have a page called create.php that creates another php file called "1". In this php file called "1". I was hoping to use

<?php echo $_SERVER['PHP_SELF'];?>

or

<?php $path = $_SERVER["SCRIPT_NAME"];echo $path;?>

To create a link that would take the number of the page and +1 it. When I do both of these functions instead of getting what I would think I would get, "1", I get "create", the page that it was created with. I'm quite dumbfounded by why this is happening, the code is most definitely on "1" and I even double checked to make sure create made a file and that I was on it so why does it think the current page is "create"?

Code being used

<?php
// start the output buffer
ob_start(); ?>
<?php echo $_SERVER['PHP_SELF'];?>
<?php
// open the cache file "cache/1" for writing
$fp = fopen("cache/1", 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
?>

You split the code in pieces and you probably have a wrong idea about what happens and what will be written in cache/1 . Your code is the same as the following:

<?php
// start the output buffer
ob_start();
// echo the path of the current script
echo $_SERVER['PHP_SELF'];

// open the cache file "cache/1" for writing
$fp = fopen("cache/1", 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();

I removed the closing PHP tag ( ?> ) when it was followed by an open PHP tag ( <?php ).

Now it should be clear that, without output buffering, the script create.php display its own path relative to the document root. The output buffering captures the output and puts it into file cache/1 .

You don't even need output buffering for this. You can simply remove all the calls to ob_* functions, remove the echo() line and use:

fwrite($fp, $_SERVER['PHP_SELF']);

It's clear that this is not your goal. You probably want to generate a PHP file that contains the following content:

<?php echo $_SERVER['PHP_SELF'];?>

This is as simple as it putting this text into a string and writing the string to the file:

<?php
$code = '<?php echo $_SERVER["PHP_SELF"];?>';
$fp = fopen("cache/1", 'w');
fwrite($fp, $code);
fclose($fp);

You can even use the PHP function file_put_contents() and all the code you posted in the question becomes:

file_put_contents('cache/1', '<?php echo $_SERVER["PHP_SELF"];?>');

If you need to put a bigger block of PHP code in the generated file then you can use the nowdoc string syntax:

$code = <<<'END_CODE'
<?php
// A lot of code here
// on multiple lines
// It is not parsed for variables and it arrives as is
// into the $code variable
$path = $_SERVER['PHP_SELF'];
echo('The path of this file is: '.$path."\n");
$newPath = dirname($path).'/'.(1+(int)basename($path));
echo('The path of next file is: '.$newPath."\n");
// That's all; there is no need for the PHP closing tag

END_CODE;

// Now, the lines 2-11 from the code above are stored verbatim in variable $code
// Put them in a file
file_put_contents('cache/1', $code);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM