php fopen,str_replace

Mic*_*el 2 php fopen str-replace

我需要打开一个文件,替换一些内容(12345和77348)并保存.到目前为止我有

$cookie_file_path=$path."/cookies/shipping-cookie".$unique; $handle = fopen($cookie_file_path, "r+"); $cookie_file_path = str_replace("12345", "77348", $cookie_file_path);

fclose($handle);

但它似乎没有工作......我将不胜感激任何帮助!

TNi*_*TNi 7

您的代码中没有任何地方可以访问您文件的内容.如果您使用的是PHP 5,则可以使用以下内容:

$cookie_file_path = $path . '/cookies/shipping-cookie' . $unique;
$content = file_get_contents($cookie_file_path);
$content = str_replace('12345', '77348', $content);
file_put_contents($cookie_file_path, $content);
Run Code Online (Sandbox Code Playgroud)

如果您使用的是PHP 4,则需要使用fopen(),fwrite()和fclose()的组合才能获得与file_put_contents()相同的效果.但是,这应该会给你一个良好的开端.