php- 只有一个生成的代码显示错误
php- just one generated code displays wrongly
我有一个代码可以列出我所有的不带扩展名的目录,而且效果很好。问题是当我想在前后添加一些 php<> 时,将我的 <?php ?>
转换为 <!-- -->
。我不知道为什么。
有什么想法吗?
<?php
$variables = '';
$handle = '';
$variablesfile = '';
// open my directory
if ($handle = opendir($_SERVER['DOCUMENT_ROOT'].'/mvi/index/graphic-documents/sozie/variables/')) {
// list directory contents
while (false !== ($variablesfile = readdir($handle))) {
// exeptions
if (($variablesfile != ".")
&& ($variablesfile != "..")
&& ($variablesfile != "index.php")
&& ($variablesfile != "compiler.php")
&& ($variablesfile != ".DS_Store"))
// only grab file names
if (is_file($_SERVER['DOCUMENT_ROOT'].'/my/path/'. $variablesfile)) {
$file_name = $variablesfile;
$file_array = explode('.',$file_name);
$extension = count($file_array) - 2;
$no_extension = substr($file_name,0,strlen($file_array[$extension]));
// creation of php code
$variables .= '<?php $'.$no_extension.' = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/'.$no_extension.'.txt") ; ?>'. "\n" ;
}
}
closedir($handle);
echo $variables ;
}
?>
检查员的结果:
应该是什么
<?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt") ; ?>
是
<!-- ?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt") ; ? -->
如果我检查 Chrome 的源代码,<?php ?>
显示良好......
其他地方一切正常..我已经检查了我的apache并且我在本地主机运行..所以那边一切正常。
请记住,在处理此请求的过程中,您只有一次拍摄和一次拍摄来呈现您需要做的任何事情。因此,您必须立即执行呈现响应所需的任何代码。
尝试通过定义关联数组来捕获结果:
$results = array();
然后使用您的 $no_extension
变量作为键将数据插入到该结构中:
$results[$no_extension] = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/$no_extension.txt");
所以最后你可以用这些数据做任何你想做的事。
不要忘记 PHP 可以并且将会在双引号字符串中插入像 $no_extension
这样的变量。如果您尝试根据其他字符串组成一个字符串,避免重复连接,这可以简化您的代码。
我有一个代码可以列出我所有的不带扩展名的目录,而且效果很好。问题是当我想在前后添加一些 php<> 时,将我的 <?php ?>
转换为 <!-- -->
。我不知道为什么。
有什么想法吗?
<?php
$variables = '';
$handle = '';
$variablesfile = '';
// open my directory
if ($handle = opendir($_SERVER['DOCUMENT_ROOT'].'/mvi/index/graphic-documents/sozie/variables/')) {
// list directory contents
while (false !== ($variablesfile = readdir($handle))) {
// exeptions
if (($variablesfile != ".")
&& ($variablesfile != "..")
&& ($variablesfile != "index.php")
&& ($variablesfile != "compiler.php")
&& ($variablesfile != ".DS_Store"))
// only grab file names
if (is_file($_SERVER['DOCUMENT_ROOT'].'/my/path/'. $variablesfile)) {
$file_name = $variablesfile;
$file_array = explode('.',$file_name);
$extension = count($file_array) - 2;
$no_extension = substr($file_name,0,strlen($file_array[$extension]));
// creation of php code
$variables .= '<?php $'.$no_extension.' = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/'.$no_extension.'.txt") ; ?>'. "\n" ;
}
}
closedir($handle);
echo $variables ;
}
?>
检查员的结果:
应该是什么
<?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt") ; ?>
是
<!-- ?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt") ; ? -->
如果我检查 Chrome 的源代码,<?php ?>
显示良好......
其他地方一切正常..我已经检查了我的apache并且我在本地主机运行..所以那边一切正常。
请记住,在处理此请求的过程中,您只有一次拍摄和一次拍摄来呈现您需要做的任何事情。因此,您必须立即执行呈现响应所需的任何代码。
尝试通过定义关联数组来捕获结果:
$results = array();
然后使用您的 $no_extension
变量作为键将数据插入到该结构中:
$results[$no_extension] = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/$no_extension.txt");
所以最后你可以用这些数据做任何你想做的事。
不要忘记 PHP 可以并且将会在双引号字符串中插入像 $no_extension
这样的变量。如果您尝试根据其他字符串组成一个字符串,避免重复连接,这可以简化您的代码。