2016-03-29 119 views
0

我已經從我的svn下載了現在存儲在本地磁盤上的文檔中的文件。大多數這些文件都是php文件。我如何閱讀位於本地磁盤上的文檔(不是「txt」),並在使用php的網站上打開它們。所以,到目前爲止,這是我,閱讀php文件

index.php 

    $(function() { 
     $('#getData').click(function(event) { 
      event.preventDefault(); 

      $.ajax({ 
       type: "GET", 
       url: "endPoint.php", 
       data : { field2_name : $('#userInput2').val() }, 
       beforeSend: function(){ 
       } 
       , complete: function(){ 
       } 
       , success: function(html){ 
        //this will add the new comment to the `comment_part` div 
        $("#displayParse").html(html); 
        //$('[name=field1_name]').val(''); 
       } 
      }); 
     }); 
    }); 

</script> 


<form id="comment_form" action="endPoint.php" method="GET"> 
    Enter the file you would like to view: 
    <input type="text" class="text_cmt" name="field2_name" id="userInput2"/> 
    <input type="submit" name="submit" value="submit" id = "getData"/> 
    <input type='hidden' name='parent_id' id='parent_id' value='0'/> 
</form> 

<div id="displayParse"> 
</div> 

endPoint.php

<?php 

$filePath = $_GET["field2_name"]; 
$url = "cs242_final/home/" . $filePath; 

$file = fopen($url, "r"); 
fread($file,filesize($url)); 

echo '<div class="comment">' . $file . '</div>'; 


?> 

基本用戶輸入他們想要打開一個文件,而這些文件位於我的本地磁盤上。由於文件內容未被打印出來,因此不確定我要出錯的位置。此外,我正在使用MAMP在本地主機上運行我的代碼。我正在使用的IDE是phpstorm。我不確定我的文檔是否需要加載到phpstorm才能訪問它們

回答

0

您沒有將值fread($file,filesize($url));分配給任何東西。嘗試:

$contents = fread($file,filesize($url)); 
echo '<div class="comment">' . $contents . '</div>'; 

此外,如果這是在生產環境中使用,確保您properly escape的文件名或壞事用戶輸入可能發生。

+0

它打印出空的:/所以它不能識別文件的內容。我擁有的文件位於本地磁盤上,這是否有所作爲? –

+0

實際上現在我得到「資源ID 3」錯誤 –

+0

這是一個MySQL錯誤,並且與您的問題無關。 – Mike