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条)

报告相同问题?

悬赏问题

  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具