str_replace of session variables php
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);
ech开发者_C百科o $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.
$resume
is a resource, you can't use it as a parametr of str_replace
. you should read the content of the file with fgets, modfy it and then write it back to work.
str_replace('\$_SESSION['creative']', $creative, $buffer);
str_replace('\$_SESSION['user']', $user, $buffer);
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?
精彩评论