2014-02-24 101 views
2

是的,我想創建一個.html或.PHP(其他)文件的一些形式。如何用PHP創建HTML文檔?

實施例:

用戶打開一個頁面(偏好:PHP),看看1種形式對網站的標題和1個按鈕。好吧,他把你的偏好的網站標題點擊按鈕。該頁面發送表單中包含的信息,並創建一個html/php文檔,其中包含用戶放置的標題。

這不是要在html中用「echo」顯示一些變量,而是要真的創建一些新的文件。

有人嗎?

+3

http://php.net/file_put_contents – Nanne

+0

它應該在哪裏保存?在服務器上? –

回答

3

這應該做你想要什麼:

$title = $_POST['title']; 
$html = '<html> 
    <head> 
     <title>' . htmlspecialchars($title) . '</title> 
    </head> 
    <body>Some HTML</body> 
</html>'; 
file_put_contents('/path/to/file.html', $html); 

這會爲你創建的標題用戶提交的(假設你通過POST提交)的文件。

+0

我會添加到這個答案OP應該知道目標文件夾的權限。 http://stackoverflow.com/questions/20283626/why-does-file-put-contents-have-permission-issues-when-run-from-the-browser?rq=1 –

+0

華友世紀XSS漏洞! –

+0

烏拉用於上傳後門 –

0

試試這個:

<?php 

$title = htmlspecialchars($_POST['title']); 
$content = "<html><head><title>$title</title></head><body>Your content here</body></html>"; 

file_put_contents('index.html', $content); 
2

所以,你的形式將需要像

<form action="" method="post"> 
    <input type="text" name="pageTitle" id="pageTitle" /> 
    <input type="submit" name="formSubmit" value="Submit" /> 
</form> 

然後,你將需要下面的代碼來捕捉頁面標題的價值,並創建HTML頁面

if($_POST['formSubmit']) { 
    $newpage = '<html><head><title>' . $_POST['pageTitle'] . '</title></head><body><-- Whatever you want to put here --></body></html>'; 
    file_put_contents('newpage.html', $newpage); 
} 
0

您將照常回顯內容,但您應該發送以下標題:

header('Content-Disposition: attachment; filename=file.html'); //or whatever name you like here 
header('Content-Type: text/html'); //or text/php if the file is php 

這將使瀏覽器中打開一個下載dialoug爲您的文件