2012-07-28 108 views
0

下面的代碼生成它包含的代碼在$開始$ result變量PHP自動生成在同一文件

$path = "./files/"; 
$path2="http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/"; 
//echo $path2; 
$folder = opendir($path); 
$start="<asx version='3.0'>\n<title>Example ASX playlist</title>"; 
$Fnm = "./playlist.php"; 
$inF = fopen($Fnm,"w"); 
fwrite($inF,$start."\n"); 
while($file = readdir($folder)) { 
if (($file != '.')&&($file != '..')&&($file != 'index.htm')){ 
$result="<entry>\n<title>$file</title>\n<ref href='$path2$file'/>\n<param name='image'  value='preview.jpg'/>\n</entry>\n"; 
    fwrite($inF,$result); 
} 
} 
fwrite($inF,"</asx>"); 
closedir($folder); 
fclose($inF); 

我想生成相同的相同的代碼一個新的PHP文件playlist.php代碼包含此代碼的文件在指定的行號 這是可能的嗎?

以上PHP腳本生成一個新文件 http://tinypaste.com/ff242cd6

+0

你想生成一個php文件,其中不包含實際的php代碼?爲什麼? – 2012-07-28 18:07:12

+0

我知道我沒有在這裏包含php代碼 這段代碼會在php標籤下面生成嗎?> – user1528878 2012-07-28 18:13:45

+0

我不明白這個問題。您能否認真解釋「相同的代碼」和「在同一個文件中」的含義? – octern 2012-07-28 18:18:54

回答

0

以下是如何追加:

$path = "./files/"; 
$path2="http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/"; 
//echo $path2; 
$folder = opendir($path); 
$start="<asx version='3.0'>\n<title>Example ASX playlist</title>"; 
$Fnm = "./playlist.php"; 
// build content 
$fileContent=$start.'/n'; 

while($file = readdir($folder)) { 
if (($file != '.')&&($file != '..')&&($file != 'index.htm')){ 
$result="<entry>\n<title>$file</title>\n<ref href='$path2$file'/>\n<param name='image'  value='preview.jpg'/>\n</entry>\n"; 
    //append to content 
$fileContent .= $result; 
} 
} 
    //append to content 
$fileContent .= "</asx>"; 

// append to file and check status 
if (file_put_contents(__FILE__ , $fileContent , FILE_APPEND) === false) 
{ 

echo "failed to put contents in ".__FILE__."<br>"; 

} 

應該這樣做......

編輯:

好吧,在我進入它,你應該先看看一些基本的可以在網上找到的PHP教程。

SO,而不是內容寫入文件,只不過是迴應了,而你呼應添加動態的東西:

$title='the title of your playlist'; 

$start="<asx version='3.0'>\n<title>".$title."</title>/n"; 

while($file = readdir($folder)) { 
if (($file != '.')&&($file != '..')&&($file != 'index.htm')){ 
$result="<entry>\n<title>$file</title>\n<ref href='$path2.$file'/>\n<param name='image'  value='preview.jpg'/>\n</entry>\n"; 
} 
} 

$endCont = $start.$result."</asx>"; 

echo $endCont; 

我創建了一個名爲$title新的變量。這個值將是輸出中生成的標題。您也可以像使用<entry>\n<title>$file</title>一樣使用其他標籤來完成此操作。

+0

是的,它的工作原理,但你確定它不適用於指定的行嗎?因爲實際上我想在某個指定行的同一個文件上的標籤之間動態生成一些代碼,所以它非常重要 – user1528878 2012-07-28 19:15:14

+0

對,爲什麼不直接打印內容而不是實際寫入文件?這樣,您可以輕鬆地在標籤之間添加動態數據等等。 – 2012-07-28 19:17:18

+0

可以舉例說明打印相同的動態代碼嗎?而不是寫 – user1528878 2012-07-28 19:37:29

0
echo highlight_file(__FILE__,true); 

http://www.php.net/manual/en/function.show-source.php

這讓你回到當前文件的源代碼下面的代碼。

+0

無法正常工作 我有index.php其中包含此代碼提到此腳本/代碼生成另一個代碼中提到的第二個代碼,但在一個單獨的PHP文件我想生成相同代碼在指定行號的index.php上,例如第10行 – user1528878 2012-07-28 18:38:37

+0

這聽起來很腥,爲什麼你需要這樣做?一般來說,我會避免動態更改腳本文件,除非它真的很重要或思維低谷,因爲它可能會造成很多問題。正如有人所說的最好的方法是創建每個副本並操縱這些副本,而不是原始文件。 – Opi 2012-07-28 18:44:27

+0

其實我想動態生成一些代碼在同一個文件上的標籤之間的某個指定的行 所以它是非常重要的 – user1528878 2012-07-28 18:51:07