【发布时间】:2011-10-17 20:02:44
【问题描述】:
我想将文件的内容加载到字符串中。该文件包含一些 php 代码,需要一些变量才能正常工作。
实现这一目标的最有效方法是什么?
这是我认为最好的方法:
使用 Session 变量:
for ($i = 0; $i < 10; $i++)
{
$_SESSION[$key] = $i;
$content .= file_get_contents($fileName);
}
然后我可以从加载的文件中访问变量。
使用 Get 方法:
for ($i = 0; $i < 10; $i++)
{
$content .= file_get_contents($fileName."?key=$i");
}
用post方法:
for ($i = 0; $i < 10; $i++)
{
$postData = http_build_query($i);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postData
)
);
$context = stream_context_create($opts);
$content .= file_get_contents($fileName, false, $context);
}
我愿意接受所有更好的方法来做到这一点。
这是一个文件内容的例子:
<?php echo $_GET['key']; /*(or $_POST or $_SESSION)*/ ?>
它会输出
0
1
2
3
4
5
6
7
8
9
【问题讨论】:
-
请解释你想要完成什么,输入/输出
-
@Phil 那是一个随机数,我只是想表明我想多次加载文件。
标签: php