我需要打开一个文件,替换一些内容(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);
最佳答案
您的代码中没有任何地方可以访问文件的内容。如果您使用的是 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);
如果您使用的是 PHP 4,则需要结合使用 fopen()、fwrite() 和 fclose() 以获得与 file_put_contents() 相同的效果。但是,这应该会给您一个良好的开端。
关于php fopen , str_replace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3200224/