Does PHP echo wait for rest of script - php

Does the php's echo wait to send data to the screen, until the rest of the script is complete?
So if I have:
<?
echo "Hello World";
$xmlData = file_get_cotents("www.webpage.com");
$myFile = "data_backup.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xmlData;
fwrite($fh, $stringData);
fclose($fh);
?>
So does the above sample what to show Hello World until the contents from the url are retrived and saved locally?

Yep. unless you flush the data with flush();

All of the scripting in PHP occurs before the page loads, so the answer to your question is: yes, it would wait until the file is retrieved and saved before showing "Hello World".

Related

To write to another text file by appending the data in codeigniter [duplicate]

This question already has answers here:
PHP fwrite new line
(4 answers)
Closed 9 years ago.
$myFile = APPPATH.'/../log.txt';
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "New Stuff 1\n";
fwrite($fh, $stringData);
$stringData = "New Stuff 2\n";
fwrite($fh, $stringData);
fclose($fh);
Above shown is my code.
output :
New Stuff 1New Stuff 2
I need New Stuff 2 in the second line
Also the newly written data should append to the existing data.How to fix the issue.
You need to use PHP Predefined Constant PHP_EOL
$stringData = "New Stuff 1" . PHP_EOL;
fwrite($fh, $stringData);
$stringData = "New Stuff 2" . PHP_EOL;
fwrite($fh, $stringData);
it will produce \r\n
file_put_content('your.txt',"data\n". PHP_EOL,FILE_APPEND);
try this.by the way if you open file in browser will will not show you next line with "\n" try to see it using view page source.or use notepad++ to se it.

Receiving post data xml

Hi I need to receive a post data which will be in an xml that is encoded in a base64 format.
I'll be receiving this from a payment gateway. Now all i get is this. My code creates a txt file but is empty. Is there anything wrong with the code? The output should be an xml envelope in a text file.
$body = '';
$fh = #fopen('php://input', 'r');
if ($fh)
{
while (!feof($fh))
{
$s = fread($fh, 1024);
echo $s;
if (is_string($s))
{
$body .= $s;
}
}
fclose($fh);
}
$body = base64_decode($body);
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $body;
fwrite($fh, $stringData);
fclose($fh);
I tried to contact the payment gateway and they are telling me that they are getting this error "The remote server returned an error: (417) Expectation failed." where could the problem exist us or them?
Since your file is returning blank, I would recommend verifying the specifications from the payment gateway for your fopen() function. In addition, if you are properly getting data back from them, then I would check the base64_decode() function. I have seen situations where there may be a header or other data at the top of the actual payload data that fouls up the base64_decode and ruins your day.
It looks like you are mixing up your file handlers, can you see if the following codes run:
$body = $HTTP_RAW_POST_DATA;
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$stringData = $body;
fwrite($ourFileHandle, $stringData);
fclose($ourFileHandle);
I have just tested the above code myself successfully.
What I suggest you do is trying the following:
Try to put a static piece of data into $body (eg: "$body = 'test';")- and see if that saves - if not then it is an issue at your end.
Delete the file (to remove any permission issues).
Double check the url that the payment gateway is sending to is correct.

PHP not writing client IP to file

I cant figure out why it wont write the client IP address to the file, everything else works.
<?php
$myFile = "ips.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $_SERVER['REMOTE_HOST']);
fclose($fh);
echo $_SERVER['REMOTE_ADDR'];
?>
Thanks in advance.
Maybe fwrite($fh, $_SERVER['REMOTE_ADDR']); ?
Why do you think that if echo $_SERVER['REMOTE_ADDR']; works,
fwrite($fh, $_SERVER['REMOTE_HOST']); should too?
Try echo $_SERVER['REMOTE_HOST']; first and see if it outputs something.
My guess: just change _HOST to _ADDR in fwrite as I said in the beginning.

Read XML data and write to new file with PHP

I may be over thinking this but are there any extra steps I am missing after reading my XML data into a string and before writing it to a local file?
header('Content-type: text/xml');
//MY URL TO A XML PAGE STYLED WITH XML STYLE SHEET
$url = "http://www.mywebsite.com/somexmlfile.xml";
//SAVE CONTENTS OF PAGE IN XML_DATA
$xml_data = file_get_contents($url);
//REMOVE STYLE SHEET INFO
$search2 = '<?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?>';
$xml_data = str_replace( $search2, "", $xml_data);
//WRITE MY DATA TO A BACKUP FILE AND THEN ECHO TO THE SCREEN FOR A FLASH APP CALLING THE INFO
$myFile = "data_backup.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xml_data;
fwrite($fh, $stringData);
fclose($fh);
echo $xml_data;
The reason I ask this is when I try opening my backup XML file in Dreamweaver it crashes everytime. I was thinking there could be an encoding issue possible.

Generating a doc file from a php "results" page?

I have a PHP generated page containing the results of a submitted form, what I would like to do is save this as a .doc file on the server.
After some googling I came across this code which I adapted:-
$myFile = "./dump/".$companyName."/testFile.doc";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);
But the problem with this is that I would have to recreate the results in order to manually write them to the file and it doesn't seem very efficient.
So I continued to google and found the PHP manual which left me scratching my head frankly, however I eventually found this:-
ob_start();
// code to generate page.
$out = ob_get_contents();
ob_end_clean();
// or write it to a file.
file_put_contents("./dump/".$companyName."/testFile.doc",$out);
Which will create the file, but doesn't write anything to it. However this seems to be the way to do what (Based on the PHP manual) I want even if I can't get it to work!
Any advice? I don't mind googling if I can figure out a decent search term :)
This sould do it for you:
$cache = 'path/to/your/file';
ob_start();
// your content goes here...
echo "hello !"; // would put hello into your file
$page = ob_get_contents();
ob_end_clean();
$fd = fopen("$cache", "w");
if ($fd) {
fwrite($fd,$page);
fclose($fd);
}
It's also a great way to cache dynamic pages. Hope it helps.

Categories