Jump to content

[SOLVED] Problem with file editor


pyrodude

Recommended Posts

Below is code I've created (Well, I borrowed the skeleton from someone else and added more functionality)

 

At one point, all of the options listed were working, but when I tried to integrate them all into one file, I have issues.

 

The only functional button, once a file is "loaded" into the editor, is the "Close File" button.  All the other buttons are essentially functioning as a close button.

 

Once I can get my buttons functional again, I plan to clean it up a bit, so fear not.  Anyway, please take a look and let me know what stupid mistake I made this time...

 

<?php 
function select_file() {
    //Establish which is the current directory (Just the current folder) and set to $value
    $dir = explode("\\",getcwd());
    $value = array_pop($dir);

    //Create a drop-down list with all of the files in the current directory (housing edit.php)
    echo "<form method=post><select name=\"file\" size=1>";
    $dir2 = scandir("..\\$value");
    foreach ($dir2 as $value2) {
        if (is_file($value2)) {
            echo "<option>$value2</option>";
        }
    }
    echo "</select><input type=\"submit\" name=\"open\" value=\"Open\"></form>";
    die;
}
if (!$_SESSION) {
    session_start();
}
echo "<html><head></head><body>";
$file = $_POST[file];
$_SESSION[file] = $file;

$loadcontent = $file; 
// Make sure a valid filename was given
if (strlen($loadcontent)>4) {
    // If save button was pressed
    if($_POST[save_file]) {
        $savecontent = stripslashes($_POST['savecontent']); 
        $fp = @fopen($loadcontent, "w"); 
        if ($fp) { 
            // Write contents of new file to $file
            fwrite($fp, $savecontent); 
            fclose($fp);
            // Refresh the page (loads the new file information)
            print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[php_SELF]\"></head><body>"; 
        } 
    } 
    // If cancel button was pressed
    elseif ($_POST[cancel]) {
        // Refresh the page (loads the old file information)
        print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[php_SELF]\"></head><body>"; 
    }
    elseif ($_POST[close]) {
        session_start();
        echo "<b>$_SESSION[file]</b> closed<br>";
        unset($_SESSION);
        unset($_POST);
        session_destroy();
        select_file();
    }
    // If rename button was pressed
    elseif ($_POST[rename]) {
        $renamefilename = $_POST[renamefilename];
        // See if that filename already exists
        if (file_exists($renamefilename)) {
            // If so, give error and refresh (loads the old file information)
            print "<html><head><META http-equiv=\"refresh\" content=\"5;URL=$_SERVER[php_SELF]\"></head><body><b>Unable to rename - $renamefilename already exists</b><br>Your page will refresh momentarily";
            exit;
        }
        else {
            // If filename doesn't exist, rename it
            rename($file,$renamefilename);
            $_SESSION[file] = $renamefilename;
            // Refresh the page and open the new filename
            print "<html><head><META http-equiv=\"refresh\" content=\"5;URL=$_SERVER[php_SELF]\"></head><body><b>$file successfully renamed to $renamefilename</b><br>Your page will refresh momentarily!";
            exit;
        }
    }
    elseif ($_POST[makenew]) {
        // If create new button was pressed
        $newfilename = $_POST[newfilename];
        // See if that filename already exists
        if (file_exists($newfilename)) {
            // If so, print error and refresh (loads old file information)
            print "<html><head><META http-equiv=\"refresh\" content=\"5;URL=$_SERVER[php_SELF]\"></head><body><b>Unable to create - $newfilename already exists</b><br>Your page will refresh momentarily";
            exit;
        }
        else {
            // If not, make a new blank file named $newfilename and refresh (loads new file information)
            $_SESSION[file] = $newfilename;
            fwrite(fopen($newfilename,w)," ");
            print "<html><head><META http-equiv=\"refresh\" content=\"5;URL=$_SERVER[php_SELF]\"></head><body><b>$newfilename successfully created!</b><br>Your page will refresh momentarily!";
            exit;
        }
    }
    // If delete button was pressed
    elseif ($_POST[delete]) {
        // Set $currself to this document's filename
        $currselfa = explode("/",$_SERVER[php_SELF]);
        $currself = array_pop($currselfa);
        // See if trying to delete the editor
        if ($file==$currself) {
            // If so, print error and reload current document
            print "<html><head><META http-equiv=\"refresh\" content=\"5;URL=$_SERVER[php_SELF]\"></head><body><b>Unable to delete $file</b><br>Your page will refresh momentarily";
            exit;
        }
        // Try to delete the current file
        if (!unlink($file)) {
            // If unable to delete, print error and refresh (loads old file information)
            print "<html><head><META http-equiv=\"refresh\" content=\"5;URL=$_SERVER[php_SELF]\"></head><body><b>Unable to delete $file</b><br>Your page will refresh momentarily";
            exit;
        }
        else {
            // If delete successful, print successful message and refresh (loads editor information)
            $_SESSION[file] = $currself;
            print "<html><head><META http-equiv=\"refresh\" content=\"5;URL=$_SERVER[php_SELF]\"></head><body><b>$file successfully deleted!</b><br>Your page will refresh momentarily!";
            exit;
        }
    }
    // If the file passed to the editor does not exist for some reason, print error
    $fp = @fopen($loadcontent, "r") or die("File <?=$file;?> does not exist!"); 
    // Set $loadcontent to the contents of $file
    $loadcontent = fread($fp, filesize($loadcontent)); 
    // Array containing all of the lines in $loadcontent
    $lines = explode("\n", $loadcontent);
    // Variable for line count on left side
    $count = count($lines);
    $loadcontent = htmlspecialchars($loadcontent); 
    fclose($fp); 
    // Add the newline character to the end of every $line
    for ($a = 1; $a < $count+1; $a++) {
        $line .= "$a\n";
    }
}
// If no filename was provided
else {
    select_file();
    die;
}
?> 
<!--- Begin form --->
<style>
div {
    background-color:#0066FF;
    padding:5px;
    border:1px solid #000000;
}
</style>
<div>
<form method=post action="<?=$_SERVER[php_SELF];?>"> 
<input type="submit" style="float:right;" name="delete" value="Delete <?=$file?>">
<input type="submit" name="makenew" value="Create a new blank document"> <input type="text" size=20 name="newfilename"><p>
<input type="submit" name="rename" value="Rename"> <input type="text" size=20 style="text-align:right;" name="renamefilename" value="<?=$file;?>">
<br><br><input type="submit" name="close" value="Close <?=$file?>">
<p> Current file: <b><?=$file; $_SERVER[file];?><b />
</div><br>
<table width="100%" valign="top" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td width="3%" align="right" valign="top"><pre style="text-align: right; padding: 4px; overflow: auto; border: 0px groove; font-size: 12px" name="lines" cols="4" rows="<?=$count+3;?>"><?=$line;?></pre></td>
    <td width="90%" align="left" valign="top"><textarea style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px; background-color:#999999;" name="savecontent" cols="120" rows="<?=$count;?>" wrap="OFF"><?=$loadcontent;?></textarea></td>
  </tr>
</table>

<br>
<p><div>
<input type="submit" name="save_file" value="Save">   
<input type="submit" name="cancel" value="Cancel"></div>
</form></body></html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.