从php中的本地磁盘读取文件
Reading in files from local disk in php
我已经从svn下载了文件,这些文件现在存储在本地磁盘上的文档中。这些文件大多是php文件。我如何读取位于本地磁盘上的文档(不是"txt"),并在使用php的网站上打开它们。到目前为止,这就是我所拥有的,
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>';
?>
基本上,用户输入一个他们想要打开的文件,这些文件位于我的本地磁盘上。不确定哪里出了问题,因为文件内容没有打印出来,而是打印出了"资源id#3"。此外,我正在使用MAMP在localhost上运行我的代码。我使用的IDE是phpstorm。我不确定我的文档是否需要加载到phpstorm上才能访问它们
fread返回您感兴趣的字符串。因此,您没有检索文件的内容,所做的基本上是打印文件php引用!试试这个:
$filecontent = fread($file,filesize($url));
echo '<div class="comment">' . $filecontent . '</div>';
$file"is"文件资源;您不想打印它,而是打印fread()的返回值,即文件的内容
但话说回来,你不想发送文件的"原始"内容,因为它可能(也可能)包含会破坏你的html结构的内容
至少你应该使用htmlspecialchar()
<?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>
您可能还对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>';
相关文章:
- 从桌面读取python文件时高亮显示代码
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 如何从本地文件夹(磁盘存储)读取或获取图像文件,并使用nodejs服务器将其发送到Angularjs
- 从php中的本地磁盘读取文件
- 使用 sql.js 从磁盘读取 SQLite 数据库
- 如何在不导致磁盘读取的情况下提供静态文件
- 读取文件到磁盘错误;名称和值是setHeader()所必需的;
- HTML5文件API readAsBinaryString读取的文件比磁盘上的文件大得多
- 使用浏览器从本地磁盘读取JSON