繁体   English   中英

PHP获取textarea的文件内容

[英]PHP get file contents for textarea

我有一个表单,可以从服务器中选择文件,然后在更改时将文件名和内容放入文本区域。 如果我在/ evo /的基本目录中保存了所有内容(如在/ evo / users / username中,是从中提取文件的位置),则此方法很好,但是当我将该页面更深地移入文件夹时,它在解析时会遇到麻烦下拉列表中的文件。 我必须添加

$_SERVER['DOCUMENT_ROOT']

对于该文件,它发现文件可以正确显示在下拉菜单中,但是,当尝试将其放入文本区域时,它将显示:

Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: GET "http://mywebsite.site/home/revo/public_html/evo/users/Addiction/Addiction.html".

这意味着我的目录在我发现它们的方式上是错误的,或者(希望并且可能在)我是如何回显它或在底部的函数更改中使用它的。 未正确加载的文本区域是显示文件内容的“ CodeValue”框。

<select size="1" name="CodeList" id="CodeList"><option selected disabled>(Select Your Code)</option>
<?php
$directory = $directory = $_SERVER['DOCUMENT_ROOT'] . '/evo/' . '/users/' . $_SESSION['username'];
$filesContents = Array();
$files = scandir( $directory ) ;

foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
$filesContents[$file] = file_get_contents($directory , $file);
echo '<option value="'. $file .'">' . $file . '</option>';
}
}
?>
</select>
            <input type="hidden" name="CodeId" id="CodeId" value="0" />
            <input type="hidden" name="CodeDescription" size="40" maxlength="50" id="CodeName" value="" />
            <font color=#33A6A6>Code:</font>&nbsp;<input name="USER" value="" SIZE=40 name="CodeValue" id="CodeValue" />&nbsp;&nbsp;&nbsp;<input type=hidden name="ACTION" value="says to"><input type=hidden name="WHOTO" value="ALL"><input type=submit value="Enter"><font size="-1"><font color=#33A6A6>Entrance:&nbsp;</font><input name="SAYS" value="Enters the room..."><font color=#33A6A6>History:&nbsp;</font><input name="HISTORY" value="20" size=2><font size="-1"><font color=#33A6A6>No Pics:&nbsp;</font><input name="NOPIC" value="1" type="checkbox" checked></form>
<script>
$(document).ready(function(){
    // apply a change event
    $('#CodeList').change(function() {
      // update input box with the currently selected value
        $('#CodeName').val($(this).val());
        $.get( '<? echo $directory ?>' + '/' + $('#CodeName').val(), function( data ) {
         $( "#CodeValue" ).val( data );
        });
  });
 });
</script>

错误消息说AJAX请求去了这个URL:

http://mywebsite.site/home/revo/public_html/evo/users/Addiction/Addiction.html

而我认为您希望它去:

http://mywebsite.site/evo/users/Addiction/Addiction.html

既然您说过/evo/是基本目录。 输出的脚本包括服务器上的内部目录路径,而不是外部可见的HTTP路径。 您可以通过在顶部执行以下操作来更改此设置:

$httpPath = '/evo/' . '/users/' . $_SESSION['username'];
$directory = $directory = $_SERVER['DOCUMENT_ROOT'] . $httpPath;

然后在脚本输出中显示:

$.get( '<? echo $httpPath ?>' + '/' + $('#CodeName').val(), function( data ) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM