2014-06-17 150 views
-1

我想通過調用函數來替換php文件中的特定字符串。 我的一切至今是:用PHP處理PHP文件

function change_data($data1, $data2) 
{ 
    //Read php file in $result 
    $result= str_replace("tag1", $data1, $result); 
    $result= str_replace("tag2", $data2, $result); 
    //Save php file 
} 

你能幫助我,並加以儲存,這樣沒有人可以訪問該文件?

編輯:我試過如下:在文件中

function change_data($data1, $data2) 
{ 
    $file = 'test.php'; 
$result = file_get_contents($file); 
    //Replace initial with test 
$result = str_replace("intial", "test", $result); 

file_put_contents($file, $result); 
} 

沒有什麼變化。

回答

-1
function change_data($data1, $data2) 
{ 
    $file = 'change_this_with_filename'; 
    $result = file_get_contents($file); 

    $result = str_replace(array("tag1", "tag2"), array($data1, $data2), $result); 

    file_put_contents($file, $result); 
} 
+0

請參閱編輯 - 不適用於我:/ –

0

它保存所以沒有其他人可以訪問該文件?

這可以是一種非常有效的方式來保護您的目錄。 public_html中的任何其他目錄都可以用相同的方式進行保護。此方法僅適用於分配給您的靜態IP地址。任何試圖使用不同IP地址瀏覽這些目錄的人都將得到403 Forbidden錯誤。

  1. 在您希望保護的目錄中,打開(或創建)名爲.htaccess的文件。 (注意在文件名開頭的圓點。)
  2. 在這個例子中,下面的代碼添加到該文件,替換100.100.100.100你計劃讓靜態IP地址:

訂單拒絕,允許 全部拒絕 允許來自100.100.100.100