daveoffy Posted March 1, 2009 Share Posted March 1, 2009 I have this text editor for my website and is in PHP. It loads the file, but when I click save, it just goes to editor.php on my site and it doesn't save. It loads everything fine. $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; if($save_file) { $savecontent = stripslashes($savecontent); $fp = @fopen($loadcontent, "w"); if ($fp) { fwrite($fp, $savecontent); fclose($fp); } } $fp = @fopen($loadcontent, "r"); $loadcontent = fread($fp, filesize($loadcontent)); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); } ?><form method=post action="<?=$_SERVER['PHP_SELF']?>"> <textarea name="savecontent" class="editor_box"><?=$loadcontent?></textarea> <br> <div style="float: right;"><input type="submit" name="save_file" value="Save"></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/ Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Your code is will not work unless register_globals is enabled, this setting is now disabled as default (as from 2002) and is soon to be removed from the upcoming release of PHP6. To fix you script $save_file should be $_POST['save_file']; $savecontent should be $_POST['savecontent']; Where is $username and $sitename coming from? Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773896 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 $username, $site, $file and all are above in the code. this code does not work, does same thing //Start loading file $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; if($_POST['save_file']) { $savecontent = stripslashes($_POST['savecontent']); $fp = @fopen($loadcontent, "w"); if ($fp) { fwrite($fp, $_POST['savecontent']); fclose($fp); } } $fp = @fopen($loadcontent, "r"); $loadcontent = fread($fp, filesize($loadcontent)); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); } ?><form method=post action="<?=$_SERVER['PHP_SELF']?>"> <textarea name="savecontent" class="editor_box"><?=$loadcontent?></textarea> <br> <div style="float: right;"><input type="submit" name="save_file" value="Save"></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773903 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 $username, $site, $file and all are above in the code. I see they are used on this line $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; But where are they defined/coming from? Variables must be defined before using them. Also you should avoid using short tags always use the full PHP syntax for tags (<?php ?>) and not <? ?>. The full syntax for <?= ?> is <?php echo ?> Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773917 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 I said it loads the files fine. I have $username set, $site set and $files set. All the content appears in the box, just it wont save. Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773918 Share on other sites More sharing options...
cooldude832 Posted March 1, 2009 Share Posted March 1, 2009 also you have some error suppressing in there and those might be problems you are missing Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773921 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 I have this, don't get any errors, it loads fine when at the url editor.php?fileid=1&site=1, than when I press save it just goes to editor.php and it doesn't save the work. THERE IS CODE UP HERE and I do start a brace so don't say remove the last brace. <?php $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; if($save_file) { $savecontent = stripslashes($savecontent); $fp = @fopen($loadcontent, "w"); if ($fp) { fwrite($fp, $savecontent); fclose($fp); } } $fp = @fopen($loadcontent, "r"); $loadcontent = fread($fp, filesize($loadcontent)); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); } ?><div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>"> <textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div> <br> <div style="float: right;"><input type="submit" name="save_file" value="Save"></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773924 Share on other sites More sharing options...
cooldude832 Posted March 1, 2009 Share Posted March 1, 2009 I have this, don't get any errors, it loads fine when at the url editor.php?fileid=1&site=1, than when I press save it just goes to editor.php and it doesn't save the work. Cause you are suppressing them! remove @ from any function Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773927 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 I removed them, same thing happens. Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773931 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 I have this, don't get any errors, it loads fine when at the url editor.php?fileid=1&site=1, than when I press save it just goes to editor.php and it doesn't save the work. THERE IS CODE UP HERE and I do start a brace so don't say remove the last brace. <?php $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; if($save_file) { $savecontent = stripslashes($savecontent); $fp = @fopen($loadcontent, "w"); if ($fp) { fwrite($fp, $savecontent); fclose($fp); } } $fp = @fopen($loadcontent, "r"); $loadcontent = fread($fp, filesize($loadcontent)); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); } ?><div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>"> <textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div> <br> <div style="float: right;"><input type="submit" name="save_file" value="Save"></div> </form> Post your code in full. Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773934 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 <?php if(isset($_GET['fileid'], $_GET['site']) && is_numeric($_GET['fileid']) && is_numeric($_GET['site'])) { $fileid = $_GET['fileid']; $siteid = $_GET['site']; $username = $_COOKIE['username']; $user_id = "SELECT * FROM users WHERE username = '$username'"; $user_id_result = mysql_query($user_id); while($user_row = mysql_fetch_array($user_id_result)) $userid = $user_row['id']; $findsite = "SELECT * FROM site WHERE site_id = '$siteid'"; $find_result = mysql_query($findsite); while($find_row = mysql_fetch_array($find_result)) $sitename = $find_row['site']; $findfile = "SELECT * FROM file WHERE fileid = '$fileid'"; $find_file_result = mysql_query($findfile); while($findfile_row = mysql_fetch_array($find_file_result)) $file = $findfile_row['filename']; //Start loading file $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; if($save_file) { $savecontent = stripslashes($savecontent); $fp = fopen($loadcontent, "w"); if ($fp) { fwrite($fp, $savecontent); fclose($fp); } } $fp = fopen($loadcontent, "r"); $loadcontent = fread($fp, filesize($loadcontent)); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); } ?><div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>"> <textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div> <br> <div style="float: right;"><input type="submit" name="save_file" value="Save"></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773937 Share on other sites More sharing options...
PFMaBiSmAd Posted March 1, 2009 Share Posted March 1, 2009 Wildteen88 already told you why the logic in your code is not evaluating as true and executing - Your code is will not work unless register_globals is enabled, this setting is now disabled as default (as from 2002) and is soon to be removed from the upcoming release of PHP6. To fix you script $save_file should be $_POST['save_file']; $savecontent should be $_POST['savecontent']; Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773970 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 Do i change ALL the $savecontents? Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-773979 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Cleaned your script up try <?php if(isset($_GET['fileid'], $_GET['site']) && is_numeric($_GET['fileid']) && is_numeric($_GET['site'])) { $fileid = $_GET['fileid']; $siteid = $_GET['site']; $username = $_COOKIE['username']; $user_id = "SELECT id FROM users WHERE username = '$username'"; $user_id_result = mysql_query($user_id); list($userid) = mysql_fetch_row($user_id_result); $findsite = "SELECT site FROM site WHERE site_id = '$siteid'"; $find_result = mysql_query($findsite); list($sitename) = mysql_fetch_row($find_result); $findfile = "SELECT filename FROM file WHERE fileid = '$fileid'"; $find_file_result = mysql_query($findfile); list($file) = mysql_fetch_row($find_file_result); $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; //Start loading file if(isset($_POST['save_file'])) { echo 'EDITING '.$loadcontent.'<br />'; echo 'READABLE: '.(is_readable($loadcontent) ? 'true' : 'false') . '<br />'; echo 'WRITABLE: '.(is_writable($loadcontent) ? 'true' : 'false') . '<br />'; $savecontent = stripslashes($_POST['savecontent']); if ($fp = fopen($loadcontent, "w")) { echo 'OPENED: '.$loadcontent.'<br />'; fwrite($fp, $savecontent); fclose($fp); echo 'CLOSED'; } else { echo 'Could not save changed to '.$loadcontent; } } $loadcontent = htmlspecialchars( file_get_contents($loadcontent) ); } ?> It should output some debug messages to see whats going on Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-774022 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 Does not show anything. Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-774028 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Woops I missed of your form. Sorry about that. <?php if(isset($_GET['fileid'], $_GET['site']) && is_numeric($_GET['fileid']) && is_numeric($_GET['site'])) { $fileid = $_GET['fileid']; $siteid = $_GET['site']; $username = $_COOKIE['username']; $user_id = "SELECT id FROM users WHERE username = '$username'"; $user_id_result = mysql_query($user_id); list($userid) = mysql_fetch_row($user_id_result); $findsite = "SELECT site FROM site WHERE site_id = '$siteid'"; $find_result = mysql_query($findsite); list($sitename) = mysql_fetch_row($find_result); $findfile = "SELECT filename FROM file WHERE fileid = '$fileid'"; $find_file_result = mysql_query($findfile); list($file) = mysql_fetch_row($find_file_result); $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file; //Start loading file if(isset($_POST['save_file'])) { echo 'EDITING '.$loadcontent.'<br />'; echo 'READABLE: '.(is_readable($loadcontent) ? 'true' : 'false') . '<br />'; echo 'WRITABLE: '.(is_writable($loadcontent) ? 'true' : 'false') . '<br />'; $savecontent = stripslashes($_POST['savecontent']); if ($fp = fopen($loadcontent, "w")) { echo 'OPENED: '.$loadcontent.'<br />'; fwrite($fp, $savecontent); fclose($fp); echo 'CLOSED'; } else { echo 'Could not save changed to '.$loadcontent; } } $loadcontent = htmlspecialchars( file_get_contents($loadcontent) ); } ?> <div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>"> <textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div> <br> <div style="float: right;"><input type="submit" name="save_file" value="Save"></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-774030 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 It does the exact same thing. It loads the content, when I click save, it goes to editor.php and does not keep the changes. Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-774034 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Change <div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>"> to <div align="center"><form method=post action="<?php echo $_SERVER['REQUEST_URI'] ?>"> The problem is when the form is submitted it is cleaning the fileid and site from the url. Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-774036 Share on other sites More sharing options...
daveoffy Posted March 1, 2009 Author Share Posted March 1, 2009 Thank you to all who has helped. (I think I could of just done that last step for it to work) But thank you for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/147446-solved-php-editor-not-working-loads-file-when-i-click-save-it-does-not/#findComment-774042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.