2010-07-08 3 views
1

파일을 열고 일부 콘텐츠 (12345를 77348)로 대체하고 저장해야합니다. 멀리까지 나는 php fopen, str_replace

$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);

그러나 그것은 작동하지 않는 것 .... 나는 어떤 도움을 주셔서 감사합니다!

+0

미안 상세한 답을 게시 할 시간이 없어,하지만 당신은 $의 cookie_file_path 아닌 파일의 실제 내용에 "77348"과 "12345"를 대체 할 않는 str_replace를 말하는 것입니다. 파일 내용을 버퍼로 읽고 THAT에서 바꾸고 파일 내용을 다시 써야합니다. – jaywon

+0

어딘가에도 fwrite를 원할 것입니다. – JAL

답변

5

코드에서 아무 것도 파일의 내용에 액세스하지 않습니다. 당신이 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()와 같은 효과를 얻으려면 그러나 이것은 당신에게 좋은 시작을 제공해야합니다.

1

내용을 바꾸지 않고 파일 이름을 바꿉니다. 작은 파일 인 경우 대신 file_get_contentsfile_put_contents을 사용할 수 있습니다.

$cookie_file_path=$path."/cookies/shipping-cookie".$unique; 
$contents = file_get_contents($cookie_file_path); 
file_put_contents($cookie_file_path, str_replace("12345", "77348", $contents)); 
1

그런 다음, 저장할 파일에 새 파일 핸들을 열고 첫 번째 파일의 내용을 읽고, 번역을 적용, 다음 두 번째 파일 핸들에 저장해야합니다

$cookie_file_path=$path."/cookies/shipping-cookie".$unique; 

# open the READ file handle 
$in_file = fopen($cookie_file_path, 'r'); 

# read the contents in 
$file_contents = fgets($in_file, filesize($cookie_file_path)); 
# apply the translation 
$file_contents = preg_replace('12345', '77348', $file_contents); 
# we're done with this file; close it 
fclose($in_file); 

# open the WRITE file handle 
$out_file = fopen($cookie_file_path, 'w'); 
# write the modified contents 
fwrite($out_file, $file_contents); 
# we're done with this file; close it 
fclose($out_file); 
0

아래 스크립트를 사용할 수 있습니다.

$ cookie_file_path = $ path '/ 쿠키/배송 쿠키'. $ 고유; $ content = file_get_contents ($ cookie_file_path); $ content = str_replace ('12345', '77348', $ content); file_put_contents ($ cookie_file_path, $ content);

감사