Jump to content

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.  I think this is probably an issue with the elseif ($_POST[<somevarhere>]) but please correct me if I'm wrong.

 

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
https://forums.phpfreaks.com/topic/60034-solved-problem-with-_post-i-think/
Share on other sites

I'm not sure if this will make a difference or not, but all your if statements look something like this:

 

elseif ($_POST[makenew]) {

 

You should be surrounding the inside of the post with quotes.

 

elseif ($_POST['makenew']) {

-------------------------------------------

 

Look at line 113:

 

$fp = @fopen($loadcontent, "r") or die("File <?=$file;?> does not exist!");

 

You closed your PHP tag, and never reopened it again. So everything after that isn't going to be executed as PHP, and you have quite a few lines under that.

 

Change that line to this:

$fp = @fopen($loadcontent, "r") or die("File $file does not exist!");

--------------------------------------------

 

EDIT:

 

This code doesn't make sense to me:

if (!$_SESSION) {
    session_start();
}

 

Why would you want to call session_start if there were NO session? session_start is for maintaining the sessions you already have set.

The $_POST[xxx] calls work without the apostrophes, but I added them in the interest of having clean code.  Thanks also for the two catches (I'm still new to sessions, so I guess I thought session_start() started a new session, rather than continuing one)

 

However, the functionality of the buttons is still nonexistent.  The code worked prior to me adding the code that selects the current file (which included adding sessions...previously I was using $_GET calls to check for ?file=file.php)

 

I don't know if there's something with my session not being initialized that's calling select_file or if my $_POSTs just aren't getting passed correctly.

 

EDIT: I added this little bit of code ($file was being set to $_POST['file'], so I thought that if $_POST['file'] wasn't set, $file would be null and thus throw everything off, but it still doesn't work...)

 

Changed

if (!$_SESSION) {
    session_start();
}
echo "<html><head></head><body>";
$file = $_POST[file];
$_SESSION[file] = $file;

 

into

 

if ($_SESSION) {
    session_start();
}
echo "<html><head></head><body>";
if (!isset($_POST['file'])) {
    // Hence, a file has already been chosen
    $file = $_SESSION['file'];
}
else {
$file = $_POST['file'];
$_SESSION['file'] = $file;
}

I added an echo print_r($_POST); to line 22, and it opens fine, but when I press one of the buttons, it seems to have all the php code in the array...?

 

Here's my updated code:

 

<?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>";
echo print_r($_POST);
if (!isset($_POST['file'])) {
    // Hence, a file has already been chosen
    $file = $_SESSION['file'];
}
else {
$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(isset($_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 (isset($_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 (isset($_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 (isset($_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 (isset($_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 (isset($_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>

Below is the code from the array (I did View Source so that I could get also include the html elements)

 

Array
(
    [delete] => Delete edit.php
    [newfilename] => 
    [renamefilename] => edit.php
    [savecontent] => <?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>\";
echo print_r($_POST);
if (!isset($_POST[\'file\'])) {
    // Hence, a file has already been chosen
    $file = $_SESSION[\'file\'];
}
else {
$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(isset($_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 (isset($_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 (isset($_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 (isset($_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 (isset($_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 (isset($_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>
)
1

 

EDIT: So it appears to be passing all of the information on the form.  I did a simple file (test.txt) and below is that output (much easier to see what's going on)

 

This is after pressing the Close button:

Array
(
    [newfilename] => 
    [renamefilename] => test.txt
    [close] => Close test.txt
    [savecontent] => This is just a test
)
1

 

And after pressing the Delete button:

Array
(
    [delete] => Delete test.txt
    [newfilename] => 
    [renamefilename] => test.txt
    [savecontent] => This is just a test
)
1

 

and (just for good measure) here's the result after pressing the Rename button

Array
(
    [newfilename] => 
    [rename] => Rename
    [renamefilename] => test.txt
    [savecontent] => This is just a test
)
1

I changed the buttons with textboxes associated with them to each be on their own form.  Now, my print_r($_POST) is only echoing the values that should be sent, but it's not performing the actions (deleting, renaming, etc).

 

So, I'm back to thinking it's an issue with the handling of my if(isset($_POST[xxx])) statements...

 

Updated form html:

 

<!--- 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?>">
</form>
<form method=post action="<?=$_SERVER['PHP_SELF'];?>"> 
<input type="submit" name="makenew" value="Create a new blank document"> <input type="text" size=20 name="newfilename"></form><p>
<form method=post action="<?=$_SERVER['PHP_SELF'];?>"><input type="submit" name="rename" value="Rename"> <input type="text" size=20 style="text-align:right;" name="renamefilename" value="<?=$file;?>"></form>
<br><br><form method=post action="<?=$_SERVER['PHP_SELF'];?>"><input type="submit" name="close" value="Close <?=$file?>"></form><form method=post action="<?=$_SERVER['PHP_SELF'];?>">
<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"></form><form method=post action="<?=$_SERVER['PHP_SELF'];?>"> 
<input type="submit" name="cancel" value="Cancel"></div>
</form></body></html>

YAY, I got it!  The issue seemed to stem from line 21.  I also removed the if($_SESSION) { session_start(); } and just added session_start to the top.  To clean it up additionally, I added the echo "<html><head></head><body> to the top of the if(isset($_POST['close'])) conditional.

 

Thank everyone very much for the help.

 

MUCH APPRECIATED!

 

Final working code:

 

<?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;
}
session_start();
if (!isset($_POST['file'])) {
    // Hence, a file has already been chosen
    $file = $_SESSION['file'];
}
else {
$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(isset($_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 (isset($_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 (isset($_POST['close'])) {
        echo "<html><head></head><body>";
        echo "<b>{$_SESSION['file']}</b> closed<br>";
        unset($_SESSION);
        unset($_POST);
        session_destroy();
        select_file();
    }
    // If rename button was pressed
    elseif (isset($_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 (isset($_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 (isset($_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?>">
</form>
<form method=post action="<?=$_SERVER['PHP_SELF'];?>"> 
<input type="submit" name="makenew" value="Create a new blank document"> <input type="text" size=20 name="newfilename"></form><p>
<form method=post action="<?=$_SERVER['PHP_SELF'];?>"><input type="submit" name="rename" value="Rename"> <input type="text" size=20 style="text-align:right;" name="renamefilename" value="<?=$file;?>"></form>
<br><br><form method=post action="<?=$_SERVER['PHP_SELF'];?>"><input type="submit" name="close" value="Close <?=$file?>"></form><form method=post action="<?=$_SERVER['PHP_SELF'];?>">
<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"></form><form method=post action="<?=$_SERVER['PHP_SELF'];?>"> 
<input type="submit" name="cancel" value="Cancel"></div>
</form></body></html>

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.