[英]Include error in writing html file from php
我的代碼似乎在這里有些問題。 它從php文件創建一個文件,但是在包含路徑上出現錯誤。
include('../include/config.php');
$name = ($_GET['createname']) ? $_GET['createname'] : $_POST['createname'];
function buildhtml($strphpfile, $strhtmlfile) {
ob_start();
include($strphpfile);
$data = ob_get_contents();
$fp = fopen ($strhtmlfile, "w");
fwrite($fp, $data);
fclose($fp);
ob_end_clean();
}
buildhtml('portfolio.php?name='.$name, "../gallery/".$name.".html");
問題似乎在這里:
'portfolio.php?name='.$name
我有什么辦法可以替換它,並且仍然將變量發送過來?
這是我在PHP擴展名后加上?name時遇到的錯誤:
Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Warning: include() [function.include]: Failed opening 'portfolio.php?name=hyundai' for inclusion (include_path='.;C:\php\pear') in D:\Projects\Metro Web\Coding\admin\create.php on line 15
現在,我在前面的答案的注釋中看到了您的代碼,我想指出的幾件事是
function buildhtml($strhtmlfile) {
ob_start(); // redundant
$fp = fopen ($strhtmlfile, "w"); // redundant
file_put_contents($strhtmlfile,
file_get_contents("http://host/portfolio.php?name={$name}"));
// where does $name come from?? ---^
close($fp); // also redundant
ob_end_clean(); // also redundant
}
buildhtml('../gallery/'.$name.'.html');
與其他許多語言一樣,在PHP中,您可以通過不同的方式進行操作。 您所做的是,您采用了三種不同的方法,僅遵循一種(絕對足夠)。 因此,當您使用函數file_put_contents()
和file_get_contents()
您不需要緩沖區,即ob_
系列函數,因為您從不讀取緩沖區中的任何內容,然后應使用ob_get_contents()
獲得該內容。 您也不需要fopen()
和fclose()
創建和使用的文件句柄,因為您從未寫入過文件句柄或從中讀取過文件句柄,即使用fwrite()
或fread()
。
如果我正確地猜測到您的功能是將html頁面復制到本地文件,則我的建議如下:
function buildhtml($dest_path, $name) {
file_put_contents($dest_path,
file_get_contents("http://host/portfolio.php?name={$name}"));
}
buildhtml('../gallery/'.$name.'.html', $name);
file_put_contents($strhtmlfile, file_get_contents("http://host/portfolio.php?name={$name}"))
可以嗎
輸出:
'portfolio.php?name='.$name, "../gallery/".$name.".html";
是:
portfolio.php?name=[your name]../gallery/[your name].html
您確定那是您想要的嗎?
PHP中的include / require語句使您可以訪問服務器上已存儲的文件中包含的代碼
您要實現的目標是包括使用特定參數在該文件中執行代碼的輸出結果
MrSil提供的建議示例使您可以請求執行這些文件中的代碼並提供參數。 之所以顯示空白頁,是因為file_put_contents“將數據保存到文件”,而file_get_contents不回顯結果,而是將其返回。 刪除file_put_contents調用,並在file_get_contents之前的行首添加回顯,它應該可以工作。
echo file_get_contents('http://domain.com/file.php?param=1');
作為警告,此方法強制執行2個獨立的PHP進程。 包含將在第一個過程中執行第二個文件的代碼。
要使包含方法起作用,您需要像最初那樣包含文件,但不指定參數。 在包含每個文件之前,您需要設置期望的參數,例如$ _GET ['name'] = $ name
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.