dongxi0523 2013-05-10 21:27
浏览 28

PHP文件编辑

I can't figure out how to open the contents of a file in a textarea and then edit the file. Don' t really have a specific question to be honest. I've been working on this for days with literally zero results. At this point I'm running out of ideas and I need a fresh pair of eyes. So friends, take a look and tell me if you see the probably glaring error in my code:

These are the file functions

    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
    }  
}

Here's where it starts:

    <?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);
    }

By the way $action == $_GET['act'] just so you know. I think that is all the relevant code. What basically happens is when I click on the edit button in my script, it only posts the [ Edit Files ] table header and that's it. So.. Idk I've been working on this for days with no results.

  • 写回答

1条回答 默认 最新

  • dongxietao0263 2013-05-11 11:33
    关注

    I revised your code to make a working example. If I just run it in the directory it is written in, it works.

    Process is like:

    1. Choose Directory,
    2. Select File
    3. Edit file text in the form
    4. Save the file

    There is no display of the permissions though, so you should add that to the selection table and make sure your web user (apache in my case) has write permission to the files.

    Your original code is really messy. You tried to combine many things into one action, and there is a mix of POST and GET variables in the same form. I suggest you refine this further to use all POST variables by removing the parts that are sent on the url in the edit form.

    <?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";
       }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示