Allenph9 Posted February 25, 2012 Share Posted February 25, 2012 So i write to an external file for my signatures storage...and dont yell at me about the joys of databases...i got it all worked out. anyways so my script changes the external file and then the external file is read and part of it is echoed on the page. Everything gets written adn everything but when i write to the file i have to refresh in order to echo the new info. I thought moving the script that changes the material in the file to the top would solve this...yet for some odd reason no change. fiddling around with it i found out that for some reason the file content dosnt actually change until I refresh. stranger still is that when I fwrite a str_replace it echos the new information just fine...hmm...help please! Quote Link to comment https://forums.phpfreaks.com/topic/257754-update-a-file-in-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 25, 2012 Share Posted February 25, 2012 Here is a truth-ism about programming - Computers are general purpose tools. What they do is completely dependent on what their program logic and data tells them to do. There are multiple different ways of writing code to perform any particular task. The code you wrote is not the only way of accomplishing what it is you are trying to do. A dozen different people could have written code to do what you are trying to do and each of their programs will likely be different, but could still produce the exact same result when it works and the exact same symptom when it does not work. The code you came up with is not uniquely the only way of doing what it is you are trying to do. We don't automatically know what your code is based on your statement of what you are doing and the description of the symptoms. If you thought we could tell what your code is and could tell you what is wrong with it or what to change in it without seeing it, you are wrong. We cannot help you with any of your code without seeing enough of your code that reproduces the symptom that you saw when you tried it. When posting code in the forum, please enclose it within the forum's tags. Edit: I have reviewed some of your posts, and you need to properly indent your code so that you can read it and so that others that you might be asking to help with it will read it as well. Quote Link to comment https://forums.phpfreaks.com/topic/257754-update-a-file-in-php/#findComment-1321080 Share on other sites More sharing options...
Allenph9 Posted February 25, 2012 Author Share Posted February 25, 2012 this is my code...the first if statement replaces my current signature in the file and echos without refreshing. The second (elseif) statement writes a news signature to the file and it does change the file...yet the signature isnt echoed until i refresh. The echo code is irrelevant since it works...it has to be the if statement...before you ask this code is WAY above the echo code. sorry about the length of the code. <?php $path_to_signatures = '../../users/users_signatures.txt'; $open_signatures_document = fopen($path_to_signatures, 'rb'); $contents_of_signatures_document = fread($open_signatures_document, filesize($path_to_signatures)); fclose($open_signatures_document); $cut_signature_head = strstr($contents_of_signatures_document, $_SESSION['the_user'] . '.<"'); $cut_signature_end = strstr($contents_of_signatures_document, '">.' . $_SESSION['the_user']); $wipe_end = str_replace($cut_signature_end, '', $cut_signature_head); $finish_wipe = str_replace($_SESSION['the_user'] . '.<"', "", $wipe_end); $test_for_existing_signature = substr_count($contents_of_signatures_document ,strtolower($_SESSION['the_user']) . '.<"') ; $illegal_characters_test = substr_count($_POST['signature'], '<'); $illegal_characters_test1 = substr_count($_POST['signature'], '>'); if (isset($_POST['change_submit2'])) { if (($_SESSION['logged_in'] == '1') && ($_POST['change_box2'] == 'CHANGE') && ($illegal_characters_test == '0') && ($illegal_characters_test1 == '0') && ($test_for_existing_signature == '1')) { $the_echo = 'You have changed your signature succesfully!'; $path_to_signatures = '../../users/users_signatures.txt'; $open_signatures_document = fopen($path_to_signatures, 'rb'); $contents_of_signatures_document = fread($open_signatures_document, filesize($path_to_signatures)); fclose($open_signatures_document); $get_front = strstr($contents_of_signatures_document, $_SESSION['the_user'] . '.<"'); $get_back = strstr($contents_of_signatures_document, '">.' . $_SESSION['the_user']); $removed_back = str_replace($get_back, '', $get_front); $finish_wipe1 = str_replace($_SESSION['the_user'] . '.<"', '', $removed_back); $old_signature = $_SESSION['the_user'] . '.<"' . $finish_wipe1 . '">.' . $_SESSION['the_user']; $new_signature = $_SESSION['the_user'] . '.<"'. $_POST['signature'] . '">.' . $_SESSION['the_user']; $path_to_signatures = '../../users/users_signatures.txt'; $open_signatures_document = fopen($path_to_signatures, "r"); $contents_of_signatures_document1 = fread($open_signatures_document, filesize($path_to_signatures)); $replace_signature = str_replace($old_signature . ' ', $new_signature, $contents_of_signatures_document) . ' '; fclose($open_signatures_document); $open_signatures_document = fopen($path_to_signatures, "w"); fwrite($open_signatures_document, $replace_signature); fclose($open_signatures_document); } elseif (($_SESSION['logged_in'] == '1') && ($_POST['change_box2'] == 'CHANGE') && ($illegal_characters_test == '0') && ($illegal_characters_test1 == '0') && ($test_for_existing_signature != '1')) { $the_echo = 'You have changed your signature succesfully!'; $path_to_signatures = '../../users/users_signatures.txt'; $open_signatures_document = fopen($path_to_signatures, 'r'); $contents_of_signatures_document = fread($open_signatures_document, filesize($path_to_signatures)); fclose($open_signatures_document); $path_to_signatures = '../../users/users_signatures.txt'; $new_sig1 = $_SESSION['the_user'] . '.<"' . $_POST['signature'] . '">.' . $_SESSION['the_user'] . ' '; $open_signatures_document = fopen($path_to_signatures, 'a'); fwrite($open_signatures_document, $new_sig1); fclose($open_signatures_document); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257754-update-a-file-in-php/#findComment-1321099 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.