简体   繁体   English

使用PHP文件创建的文件获取当前页面名称

[英]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". 因此,我有一个名为create.php的页面,该页面创建了另一个名为“ 1”的php文件。 In this php file called "1". 在这个名为“ 1”的php文件中。 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. 要创建一个链接,该链接将获取页面编号并对其进行+1。 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. 当我同时执行这两个功能时,没有得到我认为会得到的“ 1”,而是得到了“ create”(创建时使用的页面)。 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"? 我对为什么发生这种情况感到非常震惊,代码肯定在“ 1”上,我甚至仔细检查以确保create创建了一个文件,而且我在上面,所以为什么它认为当前页面是“ 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 . 您将代码分成几部分,可能对将发生什么以及将在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 ). 我删除了关闭的PHP标记( ?> ),之后是打开的PHP标记( <?php )。

Now it should be clear that, without output buffering, the script create.php display its own path relative to the document root. 现在应该清楚的是,在没有输出缓冲的情况下,脚本create.php显示其相对于文档根目录的路径。 The output buffering captures the output and puts it into file cache/1 . 输出缓冲捕获输出并将其放入文件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: 您可以简单地删除对ob_*函数的所有调用,删除echo()行并使用:

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文件:

<?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: 您甚至可以使用PHP函数file_put_contents() ,问题中发布的所有代码都将变为:

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: 如果需要在生成的文件中放入更大的PHP代码块,则可以使用nowdoc字符串语法:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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