dongzhuanlei0768 2016-03-30 00:48
浏览 187
已采纳

在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 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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译