2012-05-22 2 views
2

다른 파일의 변수를 대체해야하는 파일 작업 중입니다. 지금까지 시도 :다른 파일에서 str_replace 할 수 있습니까?

$File = "$dir/submit.php"; 
$fh = fopen($File, 'r') or die("Couldn't edit the Config-file. Please report to admin."); 
$chosendb = str_replace('$chosendb = comments;','$chosendb = wuhuws_$dir;','$chosendb'); 
fclose($fh); 

$ dir은 (는) 사용자 입력입니다. 주석은 prefix_ $ dir로 바꿔야하는 데이터베이스의 테이블입니다.

내가 뭘 잘못 했니?

+3

:

이 파일을 포함하고 수동으로 같은 이러한 변수를 설정할 수없는 어떤 이유가 있습니까. 당신은 단지 그것에 대한 손잡이를 열었을뿐입니다. ['file_get_contents()'] (http://us3.php.net/manual/en/function.file-get-contents.php)를 사용하여 그것을 문자열로 읽으십시오. 그러나 @zerkms 시작하는 것이 좋습니다 ... –

+2

@zerkms, should not * *! – bfavaretto

+0

@zerkms 어떻게 오셨습니까? :) – Christian

답변

2

파일에 다시 쓰는 것을 잊어 버리고 있습니다. Zerkms이 (을 얻거나,이 ;-) 의도) 말했듯이

// Read the file 
$content = file_get_contents("$dir/submit.php"); 

// Do the editing 
$content = str_replace('$chosendb = comments;','$chosendb = wuhuws_$dir;', $content); 

// Save the file 
file_put_contents("$dir/submit.php", $content); 

그러나, 당신이 나중에 스크립트를 디버깅 힘든 시간을 찾을 수 있습니다 특히 이후, 일반적으로 PHP와 PHP를 편집 할 수 나쁜 생각입니다 런타임시 코드가 동적으로 변경되기 때문입니다. 당신은 심지어 파일에서 읽지 않은

// Include the file  
require("$dir/submit.php"); 
// Edit the variable 
$chosendb = "wuhuws_$dir"; 
+0

어떻게하면됩니까? :) – Christian

+0

업데이트 된 답변을 확인하십시오. – Zar

+0

나는 그것에 대해 살펴볼 것이다. 감사합니다 :) – Christian

관련 문제