读取php文件


reading in a php file

我已经从svn下载了文件,这些文件现在存储在本地磁盘上的文档中。这些文件大多是php文件。我如何读取位于本地磁盘上的文档(不是"txt"),并在使用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在localhost上运行我的代码。我使用的IDE是phpstorm。我不确定我的文档是否需要加载到phpstorm上才能访问它们

您没有将fread($file,filesize($url));的值分配给任何东西。尝试:

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

此外,如果在生产环境中使用此方法,请确保正确地转义文件名的用户输入,否则可能会发生不好的事情。