問題很簡單,但我會提供一些背景信息,希望能夠更輕鬆地回答它。我的'保存文件'php代碼有什麼問題?
因此,我正在使用ELGG框架並從表單和文本框中獲取信息,希望將該數據打印到簡單的文本文件中。
不幸的是,由於某種原因,這給了我很多麻煩,我似乎無法弄清楚爲什麼無論我在哪裏看。
代碼是遵循
<?php
error_reporting(E_ALL);
//Get the page
$title = get_input('title');
$body = get_input('body');
$date = date("l jS F Y h:i A");
//remove any possible bad text characters
$FileName = $title . ' ' . $date;
//print to file
//get filename and location to save
$folderName = '/Reports/' . $FileName . '.txt';
$ReportContent = "| " . $body . " |";
//write to the file
file_put_contents($folderName, $ReportContent, 0);
//error check to see if the file now exists
if (file_put_contents($folderName, $ReportContent)) {
system_message("Report Created (" . basename($folderName) . ")");
forward(REFERER);
} else {
register_error("Report (" . basename($folderName) . ") was not saved!");
forward(REFERER);
}
那麼上面應該做的是抓住從標題和正文中的文本(我可以確認它從標題確實至少),然後將其保存在/報告/文件夾(插件的完整路徑是Automated_script_view/reports /如果需要)。然而,我總是會得到註冊錯誤,我似乎無法找到原因。
我認爲它與文件夾(/ reports /)的聲明有關,就好像我將該部分拿走一樣,它會通過並提交,儘管它似乎實際上並不存儲在任何地方。
任何和所有的建議將非常感激!
file_put_contents越來越絕對鏈接。所以它會嘗試保存到/報告中,它可能沒有權限這麼做(或者文件夾甚至不存在)。在開始處添加一個點以使其相對於執行腳本的路徑 – LordNeo
另外,你調用file_put_contents兩次,一次寫入,另一次寫入,下一次測試是否寫入,但兩次寫入都有點浪費,你應該使用第一次調用的返回值來測試結果 – waterloomatt