Jump to content

Writing output to a file


Moron

Recommended Posts

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!

 

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

<?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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.