This forum is in archive mode. You will not be able to post new content.

Author Topic: [PHP] File editing  (Read 1286 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
[PHP] File editing
« on: May 10, 2013, 10:14:56 PM »
Ok, so I debated putting this in the VIP only section because I only want intelligent responses and this is part of a bigger project I'm working on so I didn't want to release too much of the source yet but, fuck it.

So, here's my problem. 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
Code: (php) [Select]
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($handlefilesize($file));
            echo 
htmlspecialchars($contents);
        }
?>

        </textarea><input type='submit' value='Save Changes' /></form><?php
    
}   
}

Here's where it starts:
Code: (php) [Select]
<?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.
« Last Edit: May 10, 2013, 10:16:37 PM by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline relax

  • Sir
  • ***
  • Posts: 562
  • Cookies: 114
  • The one and only
    • View Profile
Re: [PHP] File editing
« Reply #1 on: May 11, 2013, 12:46:07 AM »
Hit me up on Skype and I'll give you access to http://moijo.org a project I haven't worked on for some month but I think you will get answers regarding file edit in php/js from its alpha.


too drunk to paste code here but skype you know ^_^

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [PHP] File editing
« Reply #2 on: May 16, 2013, 06:55:07 AM »
Why is it that by default it only shows the text input bar with the directory, and the table header that says File, Size, Permissions, etc? Here's what my view files function looks like now:

Code: [Select]
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 getcwd(); ?>'>
            <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 != '..')
            {
            
$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
            
}
        }
    }
}

It's only slightly different from yours. What I want it to do is display all files in the current directory and the edit, delete and chmod buttons by default, without having to click on the Go button next to the input box. That's how it used to work and even if I switch back to the exact same function that I had before it still only shows the input box with the current working directory..
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.