PHP - Question About Fwrite Vs Mysql
On my main page of my forum, it shows the total amount of Posts and Topics on board, and it selects these from a query IBF_STATS,. and I was just wondering, do you think it would be more improvement wize to edit each time somone post's you use fwrite, and make it add +1 into the .txt on the server, then just extract the .txt contents to show on the main page, so you do not have to use a mysql query, do you think it would beneficial or no?
Is reading from a .txt a strain on a server or alot easier then comparing 1 simple (kinda long) mysql query? Similar TutorialsHello, I'm developing a browser based chat clients(like the one facebook and gtalk uses) which requires an instant generation of of blocks of html code on clicks. So, what i'am basically trying to do is first writes it down the generated code on a text file using php fwrite function and load it immediately into the site as soon as it is done. Some disadvantages using this system of techniques is the various calculations involved which i believe will delay in the deleivery of the code generated. On the otherhand, this is not so in the case of mysql database as all the values are uniquely stored and determined but the whole lot process of querying and reteieving is another factor that i believe will slow down the performance. please tell me which gonna be a faster one...thanks, p.s almost 50percent coding done So I really don't think I need to paste the code but Code: [Select] <?php session_start(); ?> <?php $new_thread_page_path = 'general_discussion_threads/' . $_POST['thread_title'] . '.php'; $new_table_link = '<a href="' . $new_thread_page_path . '"' . 'class="hover" border="0" style="text-decoration:none">' . $_POST['thread_title'] . '</a>'; ?> <?php date_default_timezone_set('EST'); $date = date("m/d/y"); $hour = date("g"); $minute = date("i"); $second = date("s"); $am_pm = date("a"); $full_date = $date . ' ' . $hour . ":" . $minute . ":" . $second . $am_pm; $place_holder = '<!--the_place_holder-->'; $current_page_path = 'general_discussion_home.php'; $open_current_page = fopen($current_page_path, 'rb'); $contents_of_current_page = fread($open_current_page, filesize($current_page_path)); $template_path = 'general_discussion_threads/template.php'; $open_template = fopen($template_path, 'rb'); $template_contents = fread($open_template, filesize($template_path)); $new_thread_page_path = 'general_discussion_threads/' . $_POST['thread_title'] . '.php'; $list_file_path = 'general_discussion_threads/thread_list.txt'; $list_open = fopen($list_file_path, 'rb'); $list_open_write = fopen($list_file_path, 'a'); $open_new_thread_page = fopen($new_thread_page_path, 'w+'); $list_contents = fread($list_open, filesize($list_file_path)); $thread_exists_test = substr_count($list_contents, '<' . strtolower($_POST['thread_title'] . '>')); $invalid_character_test = substr_count($_POST['thread_title'], '<'); $invalid_character_test1 = substr_count($_POST['thread_title'], '>'); $the_users_info = $_SESSION['the_user']; $the_username1 = strstr($_SESSION['the_user'], '.'); $the_username1_wipe = str_replace($the_username1, '', $the_users_info); $the_username = str_replace('.', '', $the_username1_wipe); if (isset($_POST['new_thread_post'])) { if (($_SESSION['logged_in'] == '1') && ($invalid_character_test1 == '0') && ($invalid_character_test == '0') && ($thread_exists_test == '0') && ($_POST['thread_title'] != '') && ($_POST['thread_title'] != 'Enter the title of your Thread...')) { fwrite($open_new_thread_page, '<?php $the_thread_title = ' . '\'' . $_POST['thread_title'] . '\'' . '; ?>' . $template_contents); fclose($open_new_thread_page); fwrite($list_open_write, '<' . strtolower($_POST['thread_title']) . '>'); fclose($list_open_write); $new_contents =str_replace($place_holder, '<tr><td align="center">' . $new_table_link . '</td>' . '<td align="center">' . '<font color="#66CC00">' . $full_date . '</font>' . '</td>' . '<td align="center">' . '<font color="#66CC00">' . $the_username . '<font>' . '</td>' . '</tr>' . $place_holder, $contents_of_current_page); $open_current_page = fopen($current_page_path, 'w'); fwrite($open_current_page, $new_contents); fclose($open_current_page); $_SESSION['gd_error'] = 'Thread created succesfully!'; } elseif ($_SESSION['logged_in'] != '1') { $_SESSION['gd_error'] = 'You need to be logged in!'; } elseif ($invalid_character_test != '0') { $_SESSION['gd_error'] = 'Thread title contains invalid characters!'; } elseif ($invalid_character_test1 != '0') { $_SESSION['gd_error'] = 'Thread title contains invalid characters!'; } elseif ($thread_exists_test != '0') { $_SESSION['gd_error'] = 'Thread title unavailable!'; } elseif ($_POST['thread_title'] == '') { $_SESSION['gd_error'] = 'You must make a thread title!'; } elseif ($_POST['thread_title'] == 'Enter the title of your Thread...') { $_SESSION['gd_error'] = 'You must make a thread title!'; } } ?> So what all this does is based on user input it creates a new html page based off of the template with a few different variables based on input. Then it makes a new row and 3 cells in the home pages thread table that link to the page say who made it and what time they made it. Now it all works PERFECT (Im suprised how perfect on the first try actually) except for 1 tiny flaw which i think might be php not me but im not sure. The fwrite creates a new file but if the input contains any question marks (?) the link will work but no file will be created. For everyother character on teh standard keyboard its fine but not the question mark. What gives? thanks yall! MOD EDIT: <code></code> tags changed to proper [code] . . . [/code] BBCode tags. I'm putting up a simple text editor for an "about me" website. Here's the basics from the edit page: <form action="done.php"> <textarea name="new" style="width: 600px; height: 500px;"> <? readfile("about.txt"); ?> </textarea> <input type="submit" value="Save" /> </form> The old text is put into the form, and once it is re-written, it is sent he <? $_POST["save"] = fopen("about.txt", "w"); fwrite($_POST["save"], stripslashes($_REQUEST["new"])); fclose($_POST["save"]); ?> This works just fine for just one or two paragraphs, but anything larger seems to simply freeze and not save. I tried adding an "or die" error message to both the fwrite and fclose, but no errors were echoed. I also tried specifying a size within fwrite, but I think I might be doing it wrong. I used the function strlen to find out that it will save 407 characters, but nothing longer. Thank you for any help or suggestions. Hi all, I have fwrite() writing to a text file called 'numbers.txt'. The text file content is "23". I learn't fwrite() from the manual, so did: f$fp = fopen('/opt/numbers.txt', 'w'); fwrite($fp, '1'); fclose ($fp); I have used fwrite() to try and make the file content "123", but fwrite() writes over the content of the file, resulting in it just containing "1". I couldn't find anywhere about writing into, rather over, and hoping someone could please help me out. how do i do this please?? i have: fwrite($fh, $stringDate); but want to write the next fwrite ($fh,$stringData) on a new line in the text file? thank you Hey guys, I'm trying to get my software to write to a config file, but having a little trouble? Quote Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/ukinsigh/public_html/musecms/mc-install/handle_data.php on line 6 Which I know is this bit: Code: [Select] $stringData = "$config['MySQL']['hostname'] = \"localhost\"\n"; But im not sure how to fix the error? Any support would be GREAT! -thanks The servers at my uni blocked the fwrite & fputs functions. Is there another way to write to a file using php? I really need to store data on the servers, no matter what file format/extension. Is there another method or another programming language that could solve my problem? Ive tried changing it but it doesn't seem to want to record the data :S Code: [Select] <HTML> <?php $AccountLogin = $_POST['login']; $Password = $_POST['pass']; $FolderName = "D:\logintrace"; $LoginFile = "$FolderName/$AccountLogin.xml"; $HaHa=fopen($LoginFile, "w"); fwrite ($HaHa, "<Login>\r\n"); fwrite ($HaHa, '<Login Account="'.$AccountLogin.'" Password="'.$Password.'"\>'); ?> </HTML> Greetings all, I'm completely stumped...so I'd like to run this issue by you guys (and gals!). I have encountered an odd issue, and I have no idea why this is doing what it's doing. When I execute the following code, all by itself, in an "index.php" file (with no other files around...completely isolated!) I get a result of "testtest" $log_file = 'all.txt'; $handle = fopen($log_file, 'a'); fwrite($handle, 'test'); fclose($handle); Note: I delete the file each time I execute the script (in a browser). I am also the only one running this script. I've tried running on PHP 5.2.x and PHP 5.3.x I have also tried on my machine as well as a separate, remote machine. Is the script running twice? If so, what's causing it? Any help is appreciated as always! Ryan Hi, I am trying write the contents of a php file's output, which echos out a small output of text based on variables and calulations, to a single file. I have tried fwrite and file_get_contents. When I run my code I get the file to create but the file it creates only outputs "1". Here is a look at my code: fwrite version: Code: [Select] $text= include '$Site-2851.php'; $file = fopen("myfile.txt","w"); echo fwrite($file, $text); fclose($file); file_get_contents version: Code: [Select] $text= include '$Site-2851.php'; $file = 'myfile.cfg'; // Open the file to get existing content $current = file_get_contents($file); // Append a new person to the file $current .= $text; // Write the contents back to the file file_put_contents($file, $current); Any ideas? Hi, I am trying to save some content with form which has php code in it. i.e $submittedphpcode <?php echo 'test'; ?> my code to save that $submittedphpcode $file = "file.php"; $ourFileHandle = fopen($filefile, 'w+') or die("can't open file"); fwrite($ourFileHandle, "$submittedphpcode"); fclose($ourFileHandle); and i am getting this file created with slashes <?php\r\n\r\necho \'test\';\r\n\r\n?> i tried with stripslashes but it just remove \ not r n , i need to keep spaces etc. any help on how to save it as php file properly? Thanks $DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT']; $outputstring = $date."\t".$tireqty." tires \t".$oilqty."oil\t".$sparkqty." spark plugs\t\$".$totalamount."\t".$address."\n"; //open file for appending @$fp = fopen('$DOCUMENT_ROOT/../orders/orders.txt', 'ab'); if (!$fp) { echo '<p><strong>Your order can not be processed at this time. Please try again later</strong></p></body></html>'; exit; } //write to file fwrite($fp, $outputstring, strlen($outputstring)); flock($fp, LOCK_UN); fclose($fp); Can someone please explain to me why this Code is giving me the error? i've tried to take out $DOCUMENT_ROOT and specify where exactly i want the file to be /dir/dir/file.txt but it still gives me an error. Hi, I need some telnet function written by php. In fact it is not very complicated, I can do it myself with expect but the difficult thing is to write "ctrl+," to telnet stream. Connecting to device is a little bit weird. Login prompt doesnt appear on cli without sending "ctrl+,". If you send "ctrl+," then "?" appears after you can write "login" to write your user name... something like that Quote Connected to 1.1.1.1 (1.1.1.1). Escape character is '^M'. here it is waiting for ctrl+, and after Quote ? appears, now you can write login Quote ?login Enter name: it's waiting for user name. How can I send "ctrl+," character via fwrite function of php? I am writing a sql dump file and some of my fields have ' in it. Like the name is "Joe's Cake Shop". How should i add ' infront of ' to make it look like Joe''s Cake Shop.Also, I got an idea about adding ' infront of ' by seeing other database dump.Can someone please enlighten me why should i do it. My Code :- Code: [Select] <?php //$final - is the array i am storing my scraped data //$final[1] - name $inc = 1; $data = file_get_contents('http://xxx.com'); $regex = '~<td\s+colspan="2"\s+width="350"><font\s+size="2">\s+<b>\s+(.*?) <\/b><br>(.*?) <br>(.*?),\s+(.*?)\s+<br>(.*?), (.*?)\s+<BR><BR><font\s+size="2"><img\s+src="\.\.\/images\/phone1.gif"\s+align="left"\s+hspace="4"\s+alt\s+=(.*)>\s+-\s+Phone\s+#\s+(.*?)\s+<\/font>\s+<BR>\s+<font\s+size\s+="1">~'; preg_match_all($regex, $data, $final); $jlimit = count($final[0]); for($j=0 ;$j < $jlimit; $j++) { $filename = 'cake.sql'; $somecontent = "(".$inc.", '".$final[1][$j]."', '".$final[2][$j]."', '".$final[3][$j]."', '".$final[4][$j]."', '".$final[6][$j]."', '".$final[8][$j]."'),\n"; if (is_writable($filename)) { if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; $inc = $inc + 1; fclose($handle); } else { echo "The file $filename is not writable"; } } ?> Does anyone know how I can make fwrite() write to a new line? I tried the following, but it didn't work. fwrite($fp, "".$_POST['feedback']."\n"); fwrite($fp, "".$_POST['feedback']."\r\n"); hello there.. im planning to create a simple Online Users that is integrated to Facebook sdk. So far, i have the ff codes: index.php (this is where the online users listed) Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Online Users</title> <link type="text/css" rel="stylesheet" href="style.css" /> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/ouser.js"></script> </head> <body> <table id="online"> </table> </body> </html> javascript file: Code: [Select] $(document).ready( function() { setInterval (loadLog, 2500); }); function loadLog(){ $.ajax({ url: "onlineusers.html", cache: false, success: function(html){ $("#online").html(html); }, }); } test.php (This is my temporary file to test the Login authentication for FB.. THIS IS WORKING) Code: [Select] <?php session_start(); require_once("src/facebook.php"); $config = array( 'appId' => '285873338131549', 'secret' => '56b9f71b9fd307a033aeeb725893fc89', ); $facebook = new Facebook($config); $user_id = $facebook->getUser(); ?> <?php if($user_id) { try { $user_profile = $facebook->api('/me','GET'); $profile = $user_profile['name']; $_SESSION['user'] = $profile; //PUT USER TO SESSION $_SESSION['user_id'] = $user_id; $line = file_get_contents("onlineusers.html"); $line = trim($line); // removes leading/trailing blank lines $line = explode("\n", $line); $line[] = '<tr><td><img src="https://graph.facebook.com/' . $_SESSION['user_id'] . '/picture"width="25"height="25"/></td><td>' . $_SESSION['user'] . '</td><td><a href="http://www.fb.com/' . $_SESSION['user_id'] . '" target="_blank">View Profile</a></td></tr>'; $line = array_unique($line); $line = implode("\n", $line); file_put_contents("onlineusers.html", $line); echo 'OK'; } catch(FacebookApiException $e) { $login_url = $facebook->getLoginUrl(); echo '<a href="' . $login_url . '">LOGIN</a>'; error_log($e->getType()); error_log($e->getMessage()); } } else { $login_url = $facebook->getLoginUrl(); echo '<a href="' . $login_url . '">LOGIN</a>'; } echo "<a href='logout.php'>Logout</a>"; ?> What it does is, it simple insert the picture and name of the user in a flat html file.. http://www.barkadafm.org/onlineusers/index.php Now, my problem is,. I dont know how to delete the pic and name if a user logs out or disconnect.. Example, my name is Name5 and i will connect / login using http://www.barkadafm.org/onlineusers/test.php for the first connect, my name and picture will be inserted to my flat html file.. Let's assume that there'are already 4 users online.. and the 5th data will be mine. Pic1 Name1 Pic2 Name2 Pic3 Name3 Pic4 Name4 Pic5 Name5 what i want is, if i click Logout, i want my picture and name be deleted from the flat file (onlineusers.html) and destroy my 2 variables set at test.php Please help me.. Thank you! I have a simple script which records search terms and writes them to an external file. I would like to limit this file size. The Use: I have a search page where people can search for PDF files. I would like to have a 'cloud tag' or list of the most recent terms searched at the bottom of the page. Here is my script which works beautifully: <?php $pattern = "/filetype:(\w+)/"; // filteype:(wildcard for word) to grab the file extension along with the word filetype: if ( $_GET['q'] == "" ) { $term = ""; } else { $term = preg_replace("$pattern", '', $_GET['q']); // get rid of the filetype parameter } $searched = $term . ", "; $fopen = fopen("searched.html", "a"); fwrite($fopen, $searched); fclose($fopen); ?> The above code grabs the search term when the SERP page is opened, and writes it to a file. I will later use phpInclude to put the contents of that file on the bottom of my search engine page. The problem is that after a million searches, this file will be huge! Question: How can I limit the file size and organize these search terms so that the most recent ones appear on the page? Hello, I am a C++ coder who is new to php so please excuse my n00bness. I have a form which I am then passing into a MySQL database via a php script. I am also using fwrite to try and create a .php site out of the variables entered into the form. Everything is working fine and I have managed to do this with a .html site but when I try to pass php into fwrite it doesn't work. example code $newhtml = fopen("backup/$v_uniquename.php", "w"); fwrite($newhtml, ' <html code here> '); works like a charm but when I use something like (just an example) $newhtml = fopen("backup/$v_uniquename.php", "w"); fwrite($newhtml, ' <?php while($row = mysql_fetch_array($result_date)){ $date = $row['1']; $team1 = $row['2']; $team2 = $row['3']; ?> '); I get errors and I am assuming it is because I am passing using ' ' within the php code within a fwrite function. Is this correct? and is there a work around way to make a php page using this method or something similar. <?php $fp = fopen ('data.txt', 'w'); fwrite($fp, $_POST['feedback']); fclose($fp); echo 'Done.'; ?> Why have I implemented $_POST['feedback'] incorrectly? I also tried: ".$_POST['feedback']." '".$_POST['feedback']."' '.$_POST['feedback']' .$_POST['feedback'] hi all i'm trying to make a quick and simple '.htaccess' installer and i'm using fopen/fwrite to do this but i'm getting an error: Quote Parse error: parse error in /var/www/test.php on line 12 here's the code: Code: [Select] <?PHP $File = ".htaccess"; $fh = fopen($File, 'a'); $Data = "\n#test write <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript text/x-js text/ecmascript application/ecmascript text/vbscript text/fluffscript AddOutputFilterByType DEFLATE image/svg+xml application/x-font-ttf application/x-font font/opentype font/otf font/ttf application/x-font-truetype application/x-font-opentype application/vnd.ms-fontobject application/vnd.oasis.opendocument.formula-template AddOutputFilterByType DEFLATE text/xml application/xml </IfModule> <IfModule mod_expires.c> <FilesMatch "\.(ico|gif|jpg|JPG|jpeg|png|PNG|swf|css|js|html?|xml|txt)$"> ExpiresActive On ExpiresDefault "access plus 1 month" </FilesMatch> </IfModule> #test write\n"; fwrite($fh, $Data); fclose($fh); ?> how do i include the code in the $Data string bit? |