首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP文件编辑

PHP文件编辑
EN

Stack Overflow用户
提问于 2013-05-11 05:27:03
回答 1查看 190关注 0票数 0

我不知道如何在文本区域中打开文件的内容,然后编辑该文件。老实说,我真的没有什么特别的问题。我已经在这上面工作了几天了,几乎没有结果。在这一点上,我的想法快用完了,我需要一双新的眼睛。所以朋友们,看一看,如果你在我的代码中发现了可能的明显错误,请告诉我:

这些是文件函数

代码语言:javascript
复制
    function view_files($dir)
{?>
    <table>
        <tr>
            <form method='POST' action='?act=file&dir=<?php echo base64_encode($dir);?>'>
            <td><input type='text' name='dir' size='50' value='<?php echo $dir; ?>'>
            <input type='submit' value='Go'></form></td
        </tr>

        <table border='1'><tr>
            <td width='175'><b>Name</b></td>
            <td><b>Size</b></td>
            <td><b>Permissions</b></td>
            <td><b>Edit</b></td>
            <td><b>Delete</b></td>
            <td><b>Chmod</b></td>
        </tr>
<?php

    if($handle = opendir($dir))
    {
        while(($file = readdir($handle)) !== false)
        {
            if($file != '.' && $file != '..')
            {?>
                <table><td>
                    <?php echo $file; ?>
                    <td width='165'><a href='?act=edit&file=<?php echo base64_encode($file);?>'><b id='me'><u>Edit</u></b></a></td>
                    <td width='225'><a href='?act=del'><b id='me'><u>Delete</u></b></a></td>
                    <td width='190'><a href='?act=chmod'><b id='me'><u>Chmod</u></b></a></td>
                </td></table>
            <?php
            }
        }
    }
}

function edit_files($file)
{
    if(isset($_POST['f']) && isset($_POST['d']))
    {
        $handle = fopen($_POST['f'], 'w');
        if(!$handle)
        {
            echo 'Failed to open selected file.';
        }
        else
        {?>
            <form method='POST' action='?act=edit&file=<?php echo base64_encode($file);?>'><textarea rows='17' cols='70' name='d'><?php
            $data = htmlspecialchars(file_get_contents($_GET['edit']));
            echo $data;
            fwrite($handle, $_POST['d']);
        }
        if(file_exists($file))
        {
            $handle = fopen($file, 'r');
            $contents = fread($handle, filesize($file));
            echo htmlspecialchars($contents);
        }?>
        </textarea><input type='submit' value='Save Changes' /></form><?php
    }  
}

这是它开始的地方:

代码语言:javascript
复制
    <?php
if(isset($action) && !empty($action))
{  
    if($action == 'file')
    {?>
        <table border='1'><th><b><a id='info' href='?act=file'>[ File Management ]</a></b></th></table><?php

        view_files($dir);
    }
    elseif($action == 'edit')
    {?>
       <table border='1'><th><b>[ Edit Files ]</b></th></table><?php

       edit_files($file);
    }

顺便说一句,$action == $_GET‘==’只是想让你知道。我想这就是所有相关的代码。基本上发生的事情是,当我单击脚本中的编辑按钮时,它只发布edit Files表标题,仅此而已。所以..。Idk我已经研究这个问题好几天了,但是没有结果。

EN

回答 1

Stack Overflow用户

发布于 2013-05-11 19:33:30

我修改了你的代码来做一个工作的例子。如果我只在它被写入的目录中运行它,它就能工作。

流程如下:

  1. 选择目录,
  2. 选择文件
  3. 的形式编辑文件文本保存文件

但是没有显示权限,所以您应该将其添加到选择表中,并确保您的web用户(在我的例子中是apache)具有对这些文件的写权限。

你的原始代码真的很乱。您尝试将多个内容组合到一个操作中,而POST和GET变量的形式相同。我建议你进一步完善这一点,通过删除编辑表单中url上发送的部分来使用所有POST变量。

代码语言:javascript
复制
<?php

$dir = isset($_POST['dir'])? $_POST['dir'] : null;
$file_to_edit = isset($_GET['file'])? base64_decode($_GET['file']) : null;

?>
    <table>
        <tr>
            <form method='POST' action='?act=file&dir=<?php echo base64_encode($dir);?>'>
            <td><input type='text' name='dir' size='50' value='<?php echo $dir; ?>'>
            <input type='submit' value='Go'></form></td
        </tr>

        <table border='1'><tr>
            <td width='175'><b>Name</b></td>
            <td><b>Size</b></td>
            <td><b>Permissions</b></td>
            <td><b>Edit</b></td>
            <td><b>Delete</b></td>
            <td><b>Chmod</b></td>
        </tr>
<?php
if(!empty($dir))
    view_files($dir);

function view_files($dir)
{
    if($handle = opendir($dir))
    {
        while(($file = readdir($handle)) !== false)
        {
            if($file != '.' && $file != '..')
            {
            $path_and_file = $dir . $file;
            $encoded_path_and_file = base64_encode($path_and_file);
            ?>
                <tr>
                    <td><?php echo $file; ?></td>
                    <td><?php echo filesize($path_and_file);?></td>
                  <td>perms</td>
                  <td width='165'><a href='?act=edit&file=<?php echo $encoded_path_and_file; ?>'><b id='me'><u>Edit</u></b></a></td>
                  <td width='225'><a href='?act=del'><b id='me'><u>Delete</u></b></a></td>
                  <td width='190'><a href='?act=chmod'><b id='me'><u>Chmod</u></b></a></td>
                </tr>
            <?php
            }
        }
    }
}

if(!empty($file_to_edit)){
    echo "Editing: $file_to_edit<br>";
    edit_files($file_to_edit);
}

function edit_files($file_to_edit)
{
    if(file_exists($file_to_edit))
    {
        $handle = fopen($file_to_edit, 'r');
        $contents = fread($handle, filesize($file_to_edit));
       $data = htmlspecialchars($contents);
       $encoded_path_and_file = base64_encode($file_to_edit);
    ?>
    <form method='POST' 
            action='?f=<?php echo $encoded_path_and_file;?>'>
    <textarea rows='17' cols='70' name='d'><?php echo $data; ?></textarea><br>
    <input type='submit' value='Save Changes' />
    </form>
    <?php
    } 
    else {
        echo "File does not exist.<br>";
    }

}

if(isset($_GET['f']) && isset($_POST['d']))
    save_file();

function save_file(){
    $file_to_overwrite = base64_decode($_GET['f']);
    $handle = fopen($file_to_overwrite, 'w');
    if(!$handle)
    {
        echo 'Failed to open selected file.';
    }
    else
    {
      fwrite($handle, $_POST['d']);
      echo "Wrote d to f";
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16491068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档