2012-08-30 125 views
-2

如果我有一個名爲file.html的文件,我該如何通過PHP對這個文件做10個克隆,使它們被重命名爲file1 .... file10?製作一個html文件的克隆

這段代碼的文件,但他們都是空白時,他們應該是的mypage.html的副本(小於1KB)

<?php 


$text = file_get_contents('mypage.html'); 
for($i = 0; $i < 100; $i++) { 
    file_put_contents('file'.$i.'.html', $data); 
} 




?> 

如果我有一個名爲file.html如何使文件通過PHP這個文件的10個克隆,它們被重命名爲file1 .... file10?

這段代碼的文件,但他們都是空白時,他們應該是的mypage.html的副本(小於1KB)

+0

我只是好奇:爲了什麼目的你想克隆你的PHP文件? –

+0

他不想克隆PHP文件'mypage.html'文件 –

回答

2

看看你的變量名:

$文本 =的file_get_contents( '的mypage.html');

爲($ I = 0; $ I < 100; $ I ++){

file_put_contents( '文件' $ I,$數據。 'HTML。');

}

3

你的意思$text,而不是10100?還要確保你有權限寫你想要的文件。

$text = file_get_contents('mypage.html'); 
for($i = 1; $i <= 10; $i++) { 
    file_put_contents('file'.$i.'.html', $text); 
} 

這是最好指定dump文件夾

$text = file_get_contents('mypage.html'); 
for($i = 1; $i <= 10; $i++) { 
    file_put_contents('cloned/file'.$i.'.html', $text); 
} 

cloned文件夾PHP腳本必須具有寫權限。

0

爲什麼不使用PHP的複製功能? php copy manual

<?php 
$file = 'example.txt'; 
$newfile = 'example.txt.bak'; 

if (!copy($file, $newfile)) { 
    echo "failed to copy $file...\n"; 
} 
?> 
0

如何只使用copy()功能的循環,而不是擔心文件的內容?

$filename = 'mypage.html'; 
for ($i=1; $i < 11; $i++) { 
    copy($filename, 'file'.$i.'html'); 
} 

注意,在循環使用$i=1$i<11這將給1-10,而不是0-9值。