dongzhuanlei0768
dongzhuanlei0768
2016-03-30 00:48
浏览 146

在php中从本地磁盘读取文件

I've downloaded files from my svn that are now stored on a document on my local disk. Most of these files are php files. How can I read in documents (that aren't "txt") which are located on my local disk and open them on a website that uses php. So so far this is what I have,

index.php

<script>
$(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 = "cs_data/home/" . $filePath;

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

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


?>

basically the user inputs a file they want to open, and the files are located on my local disk. Not sure where I'm going wrong as the file contents are not being printed out and instead I'm getting this printed out "Resource id #3". Also I am running my code on localhost using MAMP. The IDE I'm using is phpstorm. I'm not sure if my documents need to be loaded onto phpstorm in order to access them

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpj83664
    dpj83664 2016-03-30 00:56
    已采纳

    $file "is" the file resource; you don't want to print that but rather the return value of fread(), i.e. the contents of the file.
    But then again you don't want to send the "raw" contents of the file, as it might (and probably will) contain something that will break your html structure.
    At the very least you should use htmlspecialchars()

    <?php
    $filePath = $_GET["field2_name"];
    
    // you really should add more security checks here
    // just imagine a request like field2_name=../../../etc/something.txt
    $url = "cs_data/home/" . $filePath;
    
    $contents = file_get_contents($url);
    echo '<div class="comment">', htmlspecialchars($contents), '</div>
    

    You might also be interested in highlight_file():

    <?php
    $filePath = $_GET["field2_name"];
    
    // you really should add more security checks here
    // just imagine a request like field2_name=../../../etc/something.txt
    $url = "cs_data/home/" . $filePath;
    
    echo '<div class="comment">';
    highlight_file($url, false);
    echo '</div>';
    
    点赞 评论
  • douqiao1413
    douqiao1413 2016-03-30 00:57

    fread returns the string you are interested in. so, you are not retrieving the content of the file, what is being done is basicaly the print of the file php reference! Try this:

    $filecontent = fread($file,filesize($url));
    
    echo '<div class="comment">' . $filecontent . '</div>';
    
    点赞 评论

相关推荐