PHP - Parsing And Writing In File - Advanced
Hello,
I need help. I have a file test.txt, and this content in it: Code: [Select] [group 1] immediate=yes links=1,14,12,22 avaliable=no [group 2] immediate=no links=1,4,14,22 Now, I also have a WEB app with database, where I can change both groups, and the only this missing is, that when a change is made in on group, that I write it to the file test.txt. I know how to write to files, but here is the problem (2 examples): If I change on the WEB page below group 1 from immediate=yes to immediate=no -> how can I change this in the file (just for group 1). If I change on the WEB page below group 2 to avaliable=yes -> how can I add this line in the file below group 2 To summarize - I have problems detecting where the group 1 or group 2 starts and end - so I only change for each group. Similar TutorialsHi, I am looking to fetch the contents of https://bb.kdg.be/webapps/blackboard/content/listContent.jsp?course_id=_98522_1&content_id=_1288143_1 in PHP However, the page works with cookies, javascript redirect, and https. Therefor I cannot manage to make a local copy of the actual contents. I have tried with curl, file_get_contents, file, fopen, and numerous user-written functions online, none of which worked. Please make sure you TEST your answer before posting it, because it is probably not as obvious as it seems! Thanks! I need to add these lines to my .htaccess file via PHP Code: [Select] RewriteCond %{REQUEST_URI} checkout RewriteRule ^(.*)$ https://myurl.com/checkout/$1 [R,L] RIGHT after this line: Code: [Select] RewriteBase / AND before this line: Code: [Select] RewriteCond %{REQUEST_FILENAME} !-f This is how the current file looks w/o the new line addition: Code: [Select] RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ index.php [L,QSA] It needs to look like this when the new lines are added(extra line breaks not necessary): Code: [Select] RewriteBase / RewriteCond %{REQUEST_URI} checkout RewriteRule ^(.*)$ https://myurl.com/checkout/$1 [R,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ index.php [L,QSA] The line numbers may not match up, so I cannot use the line number. I have a code for writing a log file... The code is working fine but it inserts the new details at the bottom of the file but i want to insert in the top... So what function should i use... Here is the file my_log.php Code: [Select] <?php $usname = $_SESSION['usname']; date_default_timezone_set('Asia/Calcutta'); $date = date("l dS \of F Y h:i:s A"); $file = "log.php"; $open = fopen($file, "a+"); fseek($open,289); fwrite($open "<b><br/>USER NAME:</b> ".$usname . "<br/>"); fwrite($open, "<b>Date & Time:</b> ".$date. "<br/>"); fwrite($open, "<b>What have they done :</b> ".$reason . "<br/><br/>"); fclose($open); ?> and here is my log.php file : Code: [Select] <?php session_start(); if($_SESSION['stage']!=1 || $_SESSION['stage2']!=2) {header('location:index.php'); die(" "); } ?> <?php if($_GET['valu']=="view_log") { $reason=" ".$_SESSION['usname']." Viewed the LOG"; include('my_log.php'); header('location:log.php'); die(""); } // bytes till here are 289 //LINE 1 : Log details have to insert here I want to insert every detail from the top of the page but just below the php code.. What should i do... ANy helo would be appreciated Pranshu Agrawal pranshu.a.11@gmail.com I know how to parse and print out contents of an XML file with SimpleXML but when I do it with this one I just get an empty string. XML file sample: Code: [Select] <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Title>Periodens vara - maj</Title> <Company>Company1</Company> <Version>1.00</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <AllowPNG/> </OfficeDocumentSettings> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>10005</WindowHeight> <WindowWidth>10005</WindowWidth> <WindowTopX>120</WindowTopX> <WindowTopY>135</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Font ss:FontName="Calibri" ss:Size="11" ss:Color="#000000"/> </Style> .... I get no error so the structure is correct. But doing print_r() returns an empty array. So does children(). How do I start, help me out I'm trying to write to a file with the following code. $fh = fopen("../inc/config.php", "a") or die("\r\nCan't open file."); $write = "\$config['database_host'] = {$mysqlh}; \$config['database_user'] = {$mysqlu}; \$config['datbase_pass'] = {$mysqlp}; \$config['datbase_name'] = {$dbn}; \$config['table_prefix'] = {$tp};"; fwrite($fh, $write); fclose($fh); It is printing out "Can't open file", which I guess means it can't find the file or I've given the wrong path. The file that is executing this code is /install and the file I'm trying to write to is /inc/config.php. I thought .. put you up a directory so if I do .. I will be at the root and then do /inc I will be in inc. Can someone please guide me on what I'm doing wrong? Thanks. I am using a script I adapted from a tutorial to print the contents of a text box to a txt file. Basically, it's a really simple way of seeing who has logged in. I only have a handful of users. The problem is, although the text file is being created in the proper folder, it isn't being written to and just remains blank. <div align="center"> <table width="300" border="2" bordercolor="#FFFFFF" style="-moz-border-radius: 18px; -webkit-border-radius: 18px;" height="120" cellpadding="0" cellspacing="0"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" background="images/loginbg.jpg" style="-moz-border-radius: 15px; -webkit-border-radius: 15px;"> <tr align="center"> <td colspan="3"><font color="#FFFFFF"><strong>Family Login </strong></font></td> </tr> <tr> <td width="78"><font color="#000000">Username</font></td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"> <?php $myusername = $_POST['myusername']; $data = "$myusername\n"; //open the file and choose the mode $fh = fopen("logs/login.txt", "a"); fwrite($fh, $data); fclose($fh); ?></td> </tr> <tr> <td><font color="#000000">Password</font></td> <td>:</td> <td><input name="mypassword" type="password" id="mypassword"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"> </td> </tr> </table> </td> </form> </tr> </table> </div> I'm not sure what's going wrong but I'm guessing it's the placing of the php, or at least some of it. I'd quite like to add the time they logged in as well. Any idea's anyone? Ok, I know how to write to a file, but what I'm looking for is to check if a line of code exists, and if it is, don't recreate it. It would also be nice to not recreate the file either. Code: [Select] $file = fopen("index.html", "w"); fwrite($file,"This is a line of text"); fclose($file); It will be a txt file and I need to parse it and insert it into a mysql, I know there are many parser examples out there, but half I don't understand...... this is how the file is... I'll also attach it... Code: [Select] [5/3/2011 12:37:14]: SuspectsKill: TeK_SiC Pwn^Guandi 9mm SMG I need to place the killer and the person who got killed, I don't really need the weapon if there not in the db I need to enter it..... and if they are I need it to add a kill on there name... this is just some of the log.. thanks for the help! Greetings,
I am new to these forums, I am working on this assignment, and these are the current issues I am running into.
Notice: Undefined variable: year in G:\EasyPHP-5.3.2i\www\PHP_Projects\ChineseZodiacs\zodiac_year_switch.php on line 77
ie. $year = validateInput($year,"Birth Year");
Notice: Undefined variable: year_count in G:\EasyPHP-5.3.2i\www\PHP_Projects\ChineseZodiacs\zodiac_year_switch.php on line 138
ie. echo "<p>You are person " . $year_count . "to enter " . $year . "</p>\n";
Honestly, I believe they are linked, because what should be happening, as the user enters the year, and hits submit, it should create a file called counts/$year.txt - $year should equal the entered data in the textbox, any help would be appreciated.
Thank you for your help.
<!DOCTYPE html> <head> <title>Write to and From a File</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <?php $dir = "counts"; if ( !file_exists($dir)) { mkdir ($dir, 0777); } function validateInput($year, $fieldname) { global $errorCount; if (empty($year)) { echo "\"$fieldname\" is a required field.<br />\n"; ++$errorCount; $retval = ""; } else { // if the field on the form has been filled in if(is_numeric($year)) { if($year >=1900 && $year <=2014) { $retval = $year; } else { ++$errorCount; echo "<p>You must enter a year between 1900 and 2014.</p>\n"; } } else { ++$errorCount; echo "<p>The year must be a number.</p>\n"; } } //ends the else for empty return($retval); } //ends the function function displayForm() { ?> <form action = "<?php echo $_SERVER['SCRIPT_NAME']; ?>" method = "post"> <p>Year of Birth: <input type="text" name="year" /></p> <p><input type="reset" value="Clear Form" /> <input type="submit" name="submit" value="Show Me My Sign" /></p> </form> <?php } function StatisticsForYear($year) { global $year_count; $counter_file = "counts/$year.txt"; if (file_exists($counter_file)) { $year_count = file_get_contents($counter_file); file_put_contents($counter_file, ++$year_count); } else { $year_count = 1; file_put_contents($counter_file, $year_count); } return ($year_count); }?> </head> <body> <?php $showForm = true; $errorCount = 0; //$year=$_POST['year']; $zodiac=""; $start_year =1900; if (isset($_POST['submit'])) $year = $_POST['year']; $year = validateInput($year,"Birth Year"); if ($errorCount==0) $showForm = false; else $showForm = true; if ($showForm == true) { //call the displayForm() function displayForm(); } else { //begins the else statement //determine the zodiac $zodiacArray = array("rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "goat", "monkey", "rooster", "dog", "pig"); switch (($_POST['year'] - $start_year) % 6) { case 0: $zodiac = $zodiacArray[0]; break; case 1: $zodiac = $zodiacArray[1]; break; case 2: $zodiac = $zodiacArray[2]; break; case 3: $zodiac = $zodiacArray[3]; break; case 4: $zodiac = $zodiacArray[4]; break; case 5: $zodiac = $zodiacArray[5]; break; case 6: $zodiac = $zodiacArray[6]; break; case 7: $zodiac = $zodiacArray[7]; break; case 8: $zodiac = $zodiacArray[8]; break; case 9: $zodiac = $zodiacArray[9]; break; case 10: $zodiac = $zodiacArray[10]; break; case 11: $zodiac = $zodiacArray[11]; break; default: echo "<p>The Zodiac for this year has not been determined.</p>\n"; break; } //ends the switch statement echo "<p>You were born under the sign of the " . $zodiac . ".</p>\n"; echo "<p>You are person " . $year_count . "to enter " . $year . "</p>\n"; } //ends the else statement ?> </body> </html> Edited by mstevens, 16 October 2014 - 06:36 PM. Hi all, I have a text file with few IP ranges in the following format: 1.52.0.0 - 1.55.255.255 What I hope I can do is to loop through the file content and retrieve the start IP / end IP (each in a variable) so that I can work on each of them. So for example for the line above I wish to get (1.52.0.0) in a var and (1.55.255.255) in another var while looping the file content So I was wondering if I can get any help on how to do this using PHP? Please excuse me if this seem to be very easy question but I am quite new to php, so any code help will be highly appreciate Thanks for your time Mark Hey Guys. I am trying to write to the file depending on which condition is met. The code works fine on my local machiene but not on my remote server. I have also tried to output any error messages to see if it would output anything, and I don't get anyting on my browser. Can anyone help me with this issue? Thanks <?php if($_SERVER['REQUEST_METHOD'] == "POST") { isset($_POST['interfax']) ? $option= "interfax" : $option= ""; isset($_POST['metrofax']) ? $option= "metrofax" : $option= ""; switch ($option) { case 'interfax': $file = "fax.php"; $fax_client = "interfax"; if(file_put_contents($file, "<?php ".'$fax_client = "' . $fax_client . '"'." ?>")) { echo "Successful"; } else { die("Can't write file"); } break; // By defualt all the orders go to metrofax so by selecting the variable it resets it self case 'metrofax': $file = "fax.php"; $fax_client = "metrofax"; file_put_contents($file, "<?php ".'$fax_client = "' . NULL . '"'." ?>"); break; } } ?> <form action="#" method="POST"> <input type="radio" name="interfax" value="interfax">Switch To Interfax<br> <input type="radio" name="metrofax" value="metrofax">Switch To Metrofax<br> <input type='submit' name="submit" > I need help! I cant get this to write the correct way! What i need is based on the value of what is posted to the script it has to write it in the config file and also make a folder! Please help Code: [Select] <?php $start = '$uploadpath=\''; $structure = '../banner/images/'.$_POST['FOLDER'].'\';\n'; $myFile = "PHP/confup.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?\n"; fwrite($fh, $stringData); $stringData = "$start $structure"; fwrite($fh, $stringData); $stringData = "?>\n"; fwrite($fh, $stringData); fclose($fh); // Desired folder structure // To create the nested structure, the $recursive parameter // to mkdir() must be specified. if (!mkdir($structure, 0777, true)) { die('Failed to create folders...'); } // ... ?> having a problem importing a file having multiple hierarchy levels!! its a csv file the contents look something like this! just need a jump start! thanks the file looks likes this row 1 row2 row3 name1 subj1 abcd efgh subje2 test1 test2 the file has to first import name1 then subj1 then abcd,then efgh then move back to subj2 and so on! Thanks Hello I am in need of writting to a txt file on a remote server. When trying I get this error: "failed to open stream: HTTP wrapper does not support writeable connections in /home....." How can I write to a siply txt file on a remote server? Hi, I am not that versed in PHP. I have been working with this snippet of code that I found on a tutorial site. It works like 97% and I get the format mostly like I need it. Except, I can not figure how to add an extra single node to the list. The code generates 20 random numbers and puts them into an array. The array is then used to create a numbers node in the xml file. It goes through and creates all the nodes. I can not seem to figure out how to put a single game numbernode at the top of all the numbers nodes. I am also not understanding how to write the output to create the actual xml file. I have written code in the past to write a string to a text file but I am not sure about this because it is in an array? The xml file will be appended every 5 minutes for data comparison down the road. I think I will need to add a routine that calls the data back in at the beginning of the script and looks for the last game number if it is there and add 1. The game number must increment each time it is appended. Any ideas on how to finish this up? The final out put will look like this. I only did a few lines instead of all 20 numbers to keep this short. Code: [Select] <?xml version="1.0"> <Container> <gamenumber>10</gamenumber> <numbers>47</numbers> <numbers>10</numbers> <numbers>5</numbers> <numbers>27</numbers> </Container> My Script Code: [Select] <?php $balls = range(1,80); shuffle($balls); $pick = array_slice($balls,1,20); //$drawn = implode(", ",$pick); $arraySize = sizeof($pick); // print ("Arraw is $drawn\n\n"); $doc = new DOMDocument(); $doc->formatOutput = true; $drawn = array(); $r = $doc->createElement( "Container" ); $doc->appendChild( $r ); for ($i=0; $i!= $arraySize; $i++){ $drawn [] = array( 'numbers' => $pick[$i], ); } foreach( $drawn as $draw ) { $numbers = $doc->createElement( "numbers" ); $numbers->appendChild( $doc->createTextNode( $draw['numbers'] ) ); $r->appendChild( $numbers); } echo $doc->saveXML(); ?> Thanks for your advise and help!!! I just wrote a php page that pulls live data from a database and presents it in an XML layout. In Internet Explorer, I can Edit, Select All, then Copy-Paste and save as XML. The resulting XML works fine. In Firefox, I can click File, Save Page As, select Text Files, then name it as an XML. Again, it works fine as XML. So..... how do I skip the Copy/Paste (IE) or Save Page As (Firefox) steps and write the output directly to an XML file? I've played with stdout(), buffers, page capturing utilities (wget, cURL, httrack, etc....), but no luck so far. This creates a pretty intense page due to the amount of data involved (1.3+ MB). Any help is appreciated! Hi, I am a newbie to php and am trying to copy mysql data to a text file. The format I need to create is Here's what I need to do: I have a page that reads file and simply echos it. $mystring = file_get_contents($file); echo "$mystring"; There is one piece of info I need to parse out and then use as part of my page title. The phrase I need to parse is an HTML comment field and looks like this: <!--PageTitle=<some text here> --> I'm sure this is simple but I'm clueless as you can tell. How can I do this? DryAquaman Hi, As I am new to PHP and JSON my Interest in the field pushes me to know further. I have successfully parsed a JSON file: { "birthdays": [ "319507200" :"Kyle", "1390435200" :"Noah", "307843200" :"Carla" ] } Using this PHP file: <?php $jsondata = file_get_contents("birthdays.json"); $json = json_decode($jsondata,true); echo $json["birthdays"][0]['1390435200']; ?> Now I would like to know how to display when Noah’s birthday is, in the form yyyy/MM/dd. Can an expert tell me how this would be done? Thank you :-) |