2012-01-03 58 views
0

我有一個文件one.php如何閱讀one.php的內容,並寫入到two.php

<?php //just a php function doen't have to do any thing with the question 
function B(){ 

} 
?> 

通過PHP我想讀one.php,因爲它是和寫入two.php,因爲它是...
注意 - '<'得到轉化爲'$lt;'

回答它是

<?php 
    $text = file_get_contents("one.php"); 
    file_put_contents("two.php", $text); 
?> 

現在還我想要的是多加一個PHP函數說function A(){}one.php內容,並將其寫入two.php

+0

什麼是你的代碼使用? – Yaniro 2012-01-03 07:52:29

+0

檢查編輯的問題 – Wazzzy 2012-01-03 07:55:28

+0

複製() – 2012-01-03 07:56:04

回答

2

這是相當簡單的時下:-)。

$text = file_get_contents("one.php"); 
file_put_contents("two.php", $text); 

更多參數的方法,請參閱file-get-contentsfile-put-contents的PHP.net文檔。

+0

我明白了..... 多數民衆贊成在工作... 你可以請幫助我出去一件事... 如何追加一個更多的功能, <?php function A(){} //從one.php中讀取函數B(){} //附加函數?>' – Wazzzy 2012-01-03 08:00:05

+0

對不起,你不完全確定你在問什麼,你想把這兩個函數調用在單獨的方法?請詳細說明你的問題。 – jakobbg 2012-01-03 08:04:41

+0

感謝您的幫助...請檢查更新的問題... – Wazzzy 2012-01-03 08:12:14

1
$contents = file_get_contents('one.php'); 
$newContents = htmlspecialchars ($contents); 
file_put_contents('two.php', $newContents); 
+0

感謝您的幫助...請檢查更新的問題... – Wazzzy 2012-01-03 08:12:05

2

如果你是「one.php」精確複製後,再使用PHP的「copy」的方法。

copy('one.php', 'two.php'); 

在問候你編輯的問題,解決的辦法是:

$content = file_get_contents('one.php'); 
$content .= 'function A() {}'; 
file_put_contents('two.php', $content, FILE_APPEND); 
+0

感謝您的幫助...請檢查更新的問題... – Wazzzy 2012-01-03 08:11:53

+0

請參閱我的答案,包括你可以在one.php中創建任何函數,並在two.php中使用它 – mat 2012-01-03 08:37:43

1

你是混合了字符串和資源一點點。 file_get_contents()將頁面存儲在一個字符串中。在這種情況下使用file_put_contents更容易。

<?php 
file_put_contents("two.php", htmlspecialchars (file_get_contents('one.php'))); 
// file_get_contents(): Stores the content of one.php into a string 
// htmlspecialchars: encodes html 
// file_put_contents(): Writes the string into two.php 
?> 

在你的情況下,更容易的解決辦法是:

<?php 
include("one.php"); 
?>