php 会话变量的 str_replace
我在 str_replace 行上收到错误。我试图用它的常量替换 $_SESSION 变量。我缺少什么?
$resume = fopen('creative/'.$_SESSION['user'].'/resume.php', 'r') or die('error when opening the file');
$fp = fopen('creative/'.$_SESSION['user'].'/resume2.php', 'w');
$creative = $_SESSION['creative'];
$user = $_SESSION['user'];
if ($resume){
while (($buffer = fgets($resume, 4096)) !== false) {
str_replace("\$_SESSION[creative]", $creative, $buffer);
str_replace("\$_SESSION[user]", $user, $buffer);
fwrite($fp, $buffer);
echo $buffer;
}
if (!feof($resume)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($resume);
fclose($fp);
}
我无法用常量替换 $_SESSION 变量。它不会在替换中返回 $_SESSION 变量的值。它似乎根本无法替代。
I am getting an error on the str_replace line. I am trying to replace the $_SESSION variable with the constant of it. What am i missing?
$resume = fopen('creative/'.$_SESSION['user'].'/resume.php', 'r') or die('error when opening the file');
$fp = fopen('creative/'.$_SESSION['user'].'/resume2.php', 'w');
$creative = $_SESSION['creative'];
$user = $_SESSION['user'];
if ($resume){
while (($buffer = fgets($resume, 4096)) !== false) {
str_replace("\$_SESSION[creative]", $creative, $buffer);
str_replace("\$_SESSION[user]", $user, $buffer);
fwrite($fp, $buffer);
echo $buffer;
}
if (!feof($resume)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($resume);
fclose($fp);
}
I can't get this to replace the $_SESSION variable with the constant. It is not returning the value of the $_SESSION variable in the replacement. it doesn't seem to replace at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$resume
是一个资源,您不能将其用作str_replace
的参数。您应该使用 fgets 读取文件的内容,对其进行修改,然后将其写回工作。$resume
is a resource, you can't use it as a parametr ofstr_replace
. you should read the content of the file with fgets, modfy it and then write it back to work.双引号导致 php 尝试读取 $_SESSION 变量本身。您正在尝试替换文件中的那些作品,对吗?
The double quotes are causing php to try and read the
$_SESSION
variable itself. You are trying to replace those works in the file, correct?