2013-12-18 33 views
0

所以我有一個簡單的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,我不希望我的代碼是最有效的(我只是想讓事情工作!)。附錄:如果您認爲要重寫代碼(而不是簡單地修復錯誤)以成爲首選的操作過程,請解釋代碼的作用,因爲我不喜歡使用代碼我不知道理解。謝謝!

+1

可能存在被隱藏的錯誤。你的'error_reporting'級別是什麼,你檢查過該頁面是否正常工作?嘗試使用'require'而不是'include',看看你是否得到致命錯誤 – casraf

+0

如上所述。我已經給你兩個語法錯誤作爲答案,但你確實需要確保你打開了錯誤報告(並且到最高級別),並學會檢查你的日誌。在這裏看到關於'error_reporting'的文檔:http://php.net/manual/en/function.error-reporting.php –

+1

@ChenAsraf我有兩個愚蠢的語法錯誤導致整個事情消滅。當你幾天沒有睡覺時,不要編寫代碼的主要原因(可悲的是,當我傾向於有我的epiphanies!)。 – Ipurgepeople

回答

4

無需訪問任何代碼或數據結構的其餘部分我可以看到2個語法錯誤...

第45行:

foreach ($pages = $page) { 

應該是:

foreach ($pages as $page) { 

88行:

echo array_shift($archiveLinks[($j-1)]; 

缺少一個支架:

echo array_shift($archiveLinks[($j-1)]); 

重要...

爲了確保你能找到這些類型的錯誤你自己,你需要確保錯誤報告被切換到一個這意味着這些會顯示給你,或者瞭解你的日誌在哪裏以及如何閱讀它們。

看到這裏的php.net的文檔:

IMO的所有開發服務器應該有錯誤報告最高級別的切換在默認情況下,讓你不會錯過任何一個錯誤,警告或通知。它只是讓你的工作變得更加簡單。在運行時設置

文檔可以在這裏找到:

+0

我覺得自己像一個完整的白癡。上個星期早上,我在接近60個小時沒有睡眠的情況下,在5點鐘的時間裏寫了這段代碼這完全解決了顯示錯誤。但是,該腳本沒有正確生成鏈接。任何想法爲什麼? – Ipurgepeople

+1

請在開發機器上將error_reporting轉爲E_ALL,我建議您學習如何進行調試並獲得適當的IDE。通過這樣做,你將節省大量時間(並且從長遠來看,金錢)。至於沒有正確顯示的鏈接,我相信如果你進行調試,你會比我們試圖從頭開始理解代碼更進一步。祝你好運! :) – casraf

+0

我同意@ChenAsraf - 現在你的直接問題是固定的我建議你去嘗試先調試自己,並回來一個新的問題,如果你找不到解決方案。 –

1

有在scripts/autodir.php此文件中的錯誤。到目前爲止,一切正常,所以這就是問題出現的地方。

你也mostlikely有隱藏的陳Asraf提到的錯誤,所以打開錯誤:

error_reporting(E_ALL); 
ini_set('display_errors', '1'); 

只要把,在PHP文件的頂部。

+0

我認爲所有開發服務器都要設置爲始終顯示所有錯誤,警告和通知非常重要。雖然這確實有助於解決眼前的問題,但我認爲值得指出,以便在幾個小時內沒有重複問題。 (這聽起來比它打算的更重要!) –

+0

我知道我至少有(一些)錯誤被打開,因爲當我上週搞砸腳本時,它吐出一堆錯誤。不過,這只是沒有做任何事情。提到的這些行打開了該文件的所有錯誤報告? – Ipurgepeople

相關問題