所以我有一個簡單的html頁面,看起來像這樣。PHP include()擦除HTML文檔的其餘部分?
<html>
<head>
<?php include("scripts/header.php"); ?>
<title>Directory</title>
</head>
<body>
<?php include("scripts/navbar.php"); ?>
<div id="phd">
<span id="ph">DIRECTORY</span>
<div id="dir">
<?php include("scripts/autodir.php"); ?>
</div>
</div>
<!--Footer Below-->
<?php include("scripts/footer.php"); ?>
<!--End Footer-->
</body>
</html>
現在,問題是,當我加載頁面,它的各種各樣的搞砸。查看頁面源代碼顯示<div id="dir">
之後的所有內容都完全無效。文件在那裏結束。沒有包含腳本,沒有</div>
的頁腳,甚至</body>
,</html>
。但它不會吐出任何錯誤。只需從包含文件中刪除文件,無需任何理由我自己或我的朋友都可以弄清楚。我們都沒有經歷過這種奇怪的行爲。
被調用的腳本是一個腳本,它將從服務器(我已上傳,而不是用戶)獲取圖片文件,並在頁面加載時自動向歸檔中的相應頁面吐出鏈接,因爲必須編輯每次我上傳一張新圖片時,目錄頁面都是真正的麻煩。
有問題的代碼如下:
<?php
//Define how many pages in each chapter.
//And define all the chapters like this.
//const CHAPTER_1 = 13; etc.
const CHAPTER_1 = 2; //2 for test purposes only.
//+-------------------------------------------------------+//
//| DON'T EDIT BELOW THIS LINE!!! |//
//+-------------------------------------------------------+//
//Defining this function for later. Thanks to an anon on php.net for this!
//This will allow me to get the constants with the $prefix prefix. In this
//case all the chapters will be defined with "CHAPTER_x" so using the prefix
//'CHAPTER' in the function will return all the chapter constants ONLY.
function returnConstants ($prefix) {
foreach (get_defined_constants() as $key=>$value) {
if (substr($key,0,strlen($prefix))==$prefix) {
$dump[$key] = $value;
}
}
if(empty($dump)) {
return "Error: No Constants found with prefix '" . $prefix . "'";
}
else {
return $dump;
}
}
//---------------------------------------------------------//
$archiveDir = "public_html/archive";
$files = array_diff(scandir($archiveDir), array("..", "."));
//This SHOULD populate the array in order, for example:
//$files[0]='20131125.png', $files[1]='20131126.png', etc.
//---------------------------------------------------------//
$pages = array();
foreach ($files as $file) {
//This parses through the files and takes only .png files to put in $pages.
$parts = pathinfo($file);
if ($parts['extension'] == "png") {
$pages[] = $file;
}
unset($parts);
}
//Now that we have our pages, let's assign the links to them.
$totalPages = count($pages);
$pageNums = array();
foreach ($pages as $page) {
//This will be used to populate the page numbers for the links.
//e.g. "<a href='archive.php?p=$pageNum'></a>"
for($i=1; $i<=$totalPages; $i++) {
$pageNums[] = $i;
}
//This SHOULD set the $pageNum array to be something like:
//$pageNum[0] = 1, $pageNum[1] = 2, etc.
}
$linkText = array();
$archiveLinks = array();
foreach ($pageNums as $pageNum) {
//This is going to cycle through each page number and
//check how to display them.
if ($totalPages < 10) {
$linkText[] = $pageNum;
}
elseif ($totalPages < 100) {
$linkText[] = "0" . $pageNum;
}
else {
$linkText[] = "00" . $pageNum;
}
}
//So, now we have the page numbers and the link text.
//Let's plug everything into a link array.
for ($i=0; $i<$totalPages; $i++) {
$archiveLinks[] = "<a href='archive.php?p=" . $pageNums[$i] . "'>" . $linkText[$i] . " " . "</a>";
//Should output: <a href= 'archive.php?p=1'>01 </a>
//as an example, of course.
}
//And now for the fun part. Let's take the links and display them.
//Making sure to automatically assign the pages to their respective chapters!
//I've tested the below using given values (instead of fetching stuff)
//and it worked fine. So I doubt this is causing it, but I kept it just in case.
$rawChapters = returnConstants('CHAPTER');
$chapters = array_values($rawChapters);
$totalChapters = count($chapters);
$chapterTitles = array();
for ($i=1; $i<=$totalChapters; $i++) {
$chapterTitles[] = "<h4>Chapter " . $i . ":</h4><p>";
echo $chapterTitles[($i-1)];
for ($j=1; $j<=$chapters[($i-1)]; $j++) {
echo array_shift($archiveLinks[($j-1)]);
}
echo "</p>"; //added to test if this was causing the deletion
}
?>
是什麼原因造成的文件的剩餘部分消失這樣呢? 編輯:兩個愚蠢的語法錯誤導致這一點,並已修復上述代碼!但是,鏈接並沒有顯示出來?請注意,我很新的PHP,我不希望我的代碼是最有效的(我只是想讓事情工作!)。附錄:如果您認爲要重寫代碼(而不是簡單地修復錯誤)以成爲首選的操作過程,請解釋代碼的作用,因爲我不喜歡使用代碼我不知道理解。謝謝!
可能存在被隱藏的錯誤。你的'error_reporting'級別是什麼,你檢查過該頁面是否正常工作?嘗試使用'require'而不是'include',看看你是否得到致命錯誤 – casraf
如上所述。我已經給你兩個語法錯誤作爲答案,但你確實需要確保你打開了錯誤報告(並且到最高級別),並學會檢查你的日誌。在這裏看到關於'error_reporting'的文檔:http://php.net/manual/en/function.error-reporting.php –
@ChenAsraf我有兩個愚蠢的語法錯誤導致整個事情消滅。當你幾天沒有睡覺時,不要編寫代碼的主要原因(可悲的是,當我傾向於有我的epiphanies!)。 – Ipurgepeople