Moron Posted March 8, 2011 Share Posted March 8, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/ Share on other sites More sharing options...
Mahngiel Posted March 8, 2011 Share Posted March 8, 2011 your wget will be #!bin/sh wget -O "http://www.google.com" > "path to save" save it as .sh and make it executable edit your crontab crontab -e [select editor] (i like nano) time diliminators *** wgetscrtip.sh Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184516 Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 The above post probably isn't what you want. Do you want the file saved on the server or the client? Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184517 Share on other sites More sharing options...
Moron Posted March 8, 2011 Author Share Posted March 8, 2011 your wget will be #!bin/sh wget -O "http://www.google.com" > "path to save" save it as .sh and make it executable edit your crontab crontab -e [select editor] (i like nano) time diliminators *** wgetscrtip.sh Thanks, but this isn't Linux-based. It's running on IIS on a Windows server. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184583 Share on other sites More sharing options...
Moron Posted March 8, 2011 Author Share Posted March 8, 2011 The above post probably isn't what you want. Do you want the file saved on the server or the client? The server. Specifically, I'll set up a folder for the files and share it out. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184585 Share on other sites More sharing options...
Mahngiel Posted March 8, 2011 Share Posted March 8, 2011 So, to clarify what you're looking for help on: 1) You have some pages on your server that displays database info 2) You wish to make a script that takes that page, 3) Parses it in an XML format, and 4) Saves it accordingly, on the server I got that correctly? Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184591 Share on other sites More sharing options...
Moron Posted March 8, 2011 Author Share Posted March 8, 2011 So, to clarify what you're looking for help on: 1) You have some pages on your server that display database from the server. 2) You wish to make a script that takes that page 3) Parses it in an XML format, and 4) Saves it accordingly, on the server I got that correctly? Almost, but drop Step #3. I already have the output displayed as the XML format we need. Once the page completes pulling data, we can Select All, Copy and Paste into (whatever editor) and save as XML. So basically, if we can just write the output straight to XML (technically, straight ASCII text would be fine because it's already formatted), that'll do the trick. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184594 Share on other sites More sharing options...
kenrbnsn Posted March 8, 2011 Share Posted March 8, 2011 Please post your current code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184599 Share on other sites More sharing options...
Mahngiel Posted March 8, 2011 Share Posted March 8, 2011 Please post your current code between tags. Ken From what i'm gathering, he's only looking to create backups that are then viewable. My skill is limited, however, I would imagine that you could do something similar to this: $tbl_backup = mysql_query("SELECT * FROM 'table"); $myFile = "backup.xml"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $tbl_backup); fclose($fh); Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184611 Share on other sites More sharing options...
Moron Posted March 8, 2011 Author Share Posted March 8, 2011 <?php $connection statement; //QUERY - See explanation below $RESULTDS=mssql_query("SELECT DISTINCT UB.[ACCOUNT_NO], UB.[ACCOUNT_NAME] FROM TABLE ORDER BY UB.[ACCOUNT_NO]"); $RESULT=mssql_fetch_assoc($RESULTDS); ?> <?php if ($RESULT['PBS_ACCOUNT_ID'] >= 00000001) { echo "<font size=2 color=#000000 face=arial> <</font>"; echo "<font size=2 color=#000000 face=arial>PBS_ACCOUNT_ID</font>"; echo "<font size=2 color=#000000 face=arial>></font>"; echo "<B>"; echo $RESULT['PBS_ACCOUNT_ID']; echo "</B>"; echo "<font size=2 color=#000000 face=arial><</font>"; echo "<font size=2 color=#000000 face=arial>/PBS_ACCOUNT_ID"; echo "<font size=2 color=#000000 face=arial>></font>"; echo "<BR>"; } else { echo "<font size=2 color=#000000 face=arial> <</font>"; echo "<font size=2 color=#000000 face=arial>PBS_ACCOUNT_ID</font>"; echo "<font size=2 color=#000000 face=arial> xsi:nil=\"true\"</font>"; echo "<font size=2 color=#000000 face=arial>/</font>"; echo "<font size=2 color=#000000 face=arial>></font>"; echo "<BR>"; } That's a snippet and the rest is more of the same. The output is laid out like the requesting agency wants in the XML schema. I just want it to write the file instead of having to copy and paste from the results page. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184666 Share on other sites More sharing options...
kenrbnsn Posted March 8, 2011 Share Posted March 8, 2011 I would put anything that needs to be in the xml file into a temporary array and then use file_put_contents() to write out the file. Something like <?php $tmp = array(); <?php if ($RESULT['PBS_ACCOUNT_ID'] >= 00000001) { $tmp[] = "<PBS_ACCOUNT_ID>{$RESULT['PBS_ACCOUNT_ID']}</PBS_ACCOUNT_ID>"; } else { $tmp[] = '<PBS_ACCOUNT_ID xsi:nil="true" />'; } // // more work adding each line to $tmp[] // file_put_contents('yourfile.xml',implode("\n",$tmp)); ?> Do not write any of the HTML code to $tmp, just the xml output. Ken Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184693 Share on other sites More sharing options...
Moron Posted March 8, 2011 Author Share Posted March 8, 2011 I would put anything that needs to be in the xml file into a temporary array and then use file_put_contents() to write out the file. Something like <?php $tmp = array(); <?php if ($RESULT['PBS_ACCOUNT_ID'] >= 00000001) { $tmp[] = "<PBS_ACCOUNT_ID>{$RESULT['PBS_ACCOUNT_ID']}</PBS_ACCOUNT_ID>"; } else { $tmp[] = '<PBS_ACCOUNT_ID xsi:nil="true" />'; } // // more work adding each line to $tmp[] // file_put_contents('yourfile.xml',implode("\n",$tmp)); ?> Do not write any of the HTML code to $tmp, just the xml output. Ken Thanks a ton! It works. Now I just have to get all of the other parameters set up. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184797 Share on other sites More sharing options...
Moron Posted March 9, 2011 Author Share Posted March 9, 2011 Okay, after playing with the code a bit tonight (I don't have the database connection at home), I got this to do what I want: <?php $tmp1 = array(); $AccountNumber = "00040"; if ($AccountNumber = 00040) { $tmp1[] = "<ACCOUNT_NO>"; $tmp1[] = "Account Number is 00040"; $tmp1[] = "</ACCOUNT_NO>"; file_put_contents('accounts.txt',implode("\n", $tmp1),FILE_APPEND); } else { $tmp1[] = "<ACCOUNT_NO>"; $tmp1[] = "Account Number is not 00040"; $tmp1[] = "</ACCOUNT_NO>"; file_put_contents('accounts.txt',implode("\n", $tmp1),FILE_APPEND); } $tmp = array(); $AccountName = "Microsoft"; if ($AccountName = "Microsoft") { $tmp[] = "<ACCOUNT_NAME>"; $tmp[] = "Account Name is Microsoft"; $tmp[] = "</ACCOUNT_NAME>"; file_put_contents('accounts.txt',implode("\n", $tmp),FILE_APPEND); } else { $tmp[] = "<ACCOUNT_NAME>"; $tmp[] = "Account Name is not Microsoft"; $tmp[] = "</ACCOUNT_NAME>"; file_put_contents('accounts.txt',implode("\n", $tmp),FILE_APPEND); } ?> The resulting output is: <ACCOUNT_NO>Account Number is 00040</ACCOUNT_NO><ACCOUNT_NAME>Account Name is Microsoft </ACCOUNT_NAME> How do I make a page break after each closing tag, like after </ACCOUNT_NO> above in the file I'm writing? Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184854 Share on other sites More sharing options...
kenrbnsn Posted March 9, 2011 Share Posted March 9, 2011 A page break doesn't make sense in a file. BTW, your "if" statements are wrong, you're using the single "=" instead of the "==". The single "=" is for assignment, the double "==" is for comparison. Also, do the file_put_contents at the very end, not within each "if" block. Ken Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184863 Share on other sites More sharing options...
jcbones Posted March 9, 2011 Share Posted March 9, 2011 You do know you can save in a .xml file right? If you just want the data output for another program to use, you could just set the header to recognize the .php file as a .xml file. header('Content-type: text/xml'); Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184864 Share on other sites More sharing options...
Moron Posted March 9, 2011 Author Share Posted March 9, 2011 A page break doesn't make sense in a file. BTW, your "if" statements are wrong, you're using the single "=" instead of the "==". The single "=" is for assignment, the double "==" is for comparison. Also, do the file_put_contents at the very end, not within each "if" block. Ken A couple of things.... if I put the file_put_contents only at the end, it only gives me the results of the last "if" statement. What am I doing wrong there? I need the page break because each set of <PARAMETERS></PARAMETERS> needs to be enclosed. Plus, I need easier readability. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184878 Share on other sites More sharing options...
trinaryoc Posted March 9, 2011 Share Posted March 9, 2011 This might work for you but your folder permissions need to be set to 755. <?php $tmp1 = array(); $tmp = array(); $AccountNumber = "00040"; $myFile = "yourfile.xml" $fh = fopen($myFile, 'a') or die("can't open file"); if ($AccountNumber == 00040) { $tmp1[] = "<ACCOUNT_NO>"; $tmp1[] = "Account Number is 00040"; $tmp1[] = "</ACCOUNT_NO>\r\n"; } else { $tmp1[] = "<ACCOUNT_NO>"; $tmp1[] = "Account Number is not 00040"; $tmp1[] = "</ACCOUNT_NO>\r\n"; } $tmp = array(); $AccountName = "Microsoft"; if ($AccountName == "Microsoft") { $tmp[] = "<ACCOUNT_NAME>"; $tmp[] = "Account Name is Microsoft"; $tmp[] = "</ACCOUNT_NAME>\r\n"; } else { $tmp[] = "<ACCOUNT_NAME>"; $tmp[] = "Account Name is not Microsoft"; $tmp[] = "</ACCOUNT_NAME>\r\n"; } fwrite($fh, $tmp1); fwrite($fh, $tmp); fclose($fh); ?> you also didnt define your second array. using "\r\n" will create your line breaks for ya. Questions Do you need arrays to hold your strings? Are you really appending or over-writing your file each time? Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184879 Share on other sites More sharing options...
Moron Posted March 9, 2011 Author Share Posted March 9, 2011 You do know you can save in a .xml file right? If you just want the data output for another program to use, you could just set the header to recognize the .php file as a .xml file. header('Content-type: text/xml'); Yeah, I know, but I'm just keeping it as simple as possible for my testing tonight. If I can get this right, I'll switch it back to writing XML. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184880 Share on other sites More sharing options...
kenrbnsn Posted March 9, 2011 Share Posted March 9, 2011 Put <?php $tmp = array(); ?> Before all the if statements. Only use "$tmp[] = ", not "$tmp1[] = " and use <?php file_put_contents('filename.xml',implode("\n",$tmp)); ?> at the end of the script. If you want to separate the sections for easier readability, you can use <?php $tmp[] = ''; ?> after you close each section. This would put a blank line in the file. Ken Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184882 Share on other sites More sharing options...
Moron Posted March 9, 2011 Author Share Posted March 9, 2011 Put <?php $tmp = array(); ?> Before all the if statements. Only use "$tmp[] = ", not "$tmp1[] = " and use <?php file_put_contents('filename.xml',implode("\n",$tmp)); ?> at the end of the script. If you want to separate the sections for easier readability, you can use <?php $tmp[] = ''; ?> after you close each section. This would put a blank line in the file. Ken Here's my entire code: <?php $tmp = array(); ?> <?php $AccountNumber = "00040"; if ($AccountNumber = 00040) { $tmp[] = "<ACCOUNT_NO>"; $tmp[] = "Account Number is 00040"; $tmp[] = "</ACCOUNT_NO>"; } else { $tmp[] = "<ACCOUNT_NO>"; $tmp[] = "Account Number is not 00040"; $tmp[] = "</ACCOUNT_NO>"; } ?> <?php $tmp = array(); ?> <?php $AccountName = "Microsoft"; if ($AccountName = "Microsoft") { $tmp[] = "<ACCOUNT_NAME>"; $tmp[] = "Account Name is Microsoft"; $tmp[] = "</ACCOUNT_NAME>"; } else { $tmp[] = "<ACCOUNT_NAME>"; $tmp[] = "Account Name is not Microsoft"; $tmp[] = "</ACCOUNT_NAME>"; } ?> <?php file_put_contents('accounts.txt',implode("\n",$tmp)); ?> It only gives me: <ACCOUNT_NAME>Account Name is Microsoft</ACCOUNT_NAME> It only gives me the <ACCOUNT_NO> tags and value if I add the file_put_contents within each IF section. EDIT: I'm using "accounts.txt" just for simplicity. Notepad is quick and easy. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184885 Share on other sites More sharing options...
kenrbnsn Posted March 9, 2011 Share Posted March 9, 2011 Remove the second <?php $tmp = array(); ?> since it is wiping out anything you've already stored in the $tmp array. Ken Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184901 Share on other sites More sharing options...
Moron Posted March 9, 2011 Author Share Posted March 9, 2011 Remove the second <?php $tmp = array(); ?> since it is wiping out anything you've already stored in the $tmp array. Ken Thanks! That did it. There is still one issue, though. This... <?php $tmp[] = ''; ?> ...isn't putting in a page break. In fact, I can't tell that it does anything. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184982 Share on other sites More sharing options...
HuggieBear Posted March 9, 2011 Share Posted March 9, 2011 I assume you mean a line break, not a page break. Try changing these lines: $tmp[] = "</ACCOUNT_NO>"; $tmp[] = "</ACCOUNT_NAME>"; to these: $tmp[] = "</ACCOUNT_NO>\n"; $tmp[] = "</ACCOUNT_NAME>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1184984 Share on other sites More sharing options...
Moron Posted March 9, 2011 Author Share Posted March 9, 2011 I assume you mean a line break, not a page break. Try changing these lines: $tmp[] = "</ACCOUNT_NO>"; $tmp[] = "</ACCOUNT_NAME>"; to these: $tmp[] = "</ACCOUNT_NO>\n"; $tmp[] = "</ACCOUNT_NAME>\n"; Yeah, that's what I meant, the same as a "<BR>" tag in HTML. But the above didn't work. It kept everything on one line. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1185001 Share on other sites More sharing options...
HuggieBear Posted March 9, 2011 Share Posted March 9, 2011 Can you post your most recent code now. Quote Link to comment https://forums.phpfreaks.com/topic/229987-writing-output-to-a-file/#findComment-1185003 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.