How to output to screen and a file at the same time?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

How to output to screen and a file at the same time?

Post by becky-atlanta »

echo is to write to the screen.

fwrite is to write to a file.

Is there a command you can do both at the same time?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to output to screen and a file at the same time?

Post by requinix »

No.

So... any reason why you can't use those?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to output to screen and a file at the same time?

Post by Benjamin »

Code: Select all

 
 
function echoWrite($string, $file) {
    echo $string;
    $fh = fopen($file, ...
}
 
 
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to output to screen and a file at the same time?

Post by becky-atlanta »

I thought about creating a function to do that but I still do not have a handle on it. I am not clear how it works. Examples I saw just confused me.

I will try your function when I have more time. I need to catch a flight this morning.

Thank you for playing!
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to output to screen and a file at the same time?

Post by becky-atlanta »

Can you explain your function in more details?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: How to output to screen and a file at the same time?

Post by susrisha »

Code: Select all

 
/*
 *Function to write both into a file and on to a screen
 * @param : $string : The string to be written into and printed on screen
 * @param : $file : the full path of the file to write the string into
*/
 
 
function echoWrite($string, $file) {
    echo $string;         //outputs the string onto the screen
    $fh = fopen($file, "a");  //opens the file in append mode
   fwrite($string,$fh);      //writes the string onto the file
   fclose($fh);               //closes the file handle
}
 
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to output to screen and a file at the same time?

Post by becky-atlanta »

Are you really in India? You are so far away. Thank you for your help.

I was using something very close to what you gave me. I used your code to test.
---------------------------------------------
<?php
function echoWrite($string, $file) {
echo $string;
$fh = fopen($file, "a");
fwrite($string,$fh);
fclose($fh);
}

echoWrite("boys n girls", "yyyy.txt");
echoWrite("what about me?", "yyyy.txt");
echoWrite("I am a lucky girl.", "yyyy.txt");
?>
---------------------------------------------
I am still getting the following error:

boys n girls
Warning: fwrite(): supplied argument is not a valid stream resource in line 12
what about me?
Warning: fwrite(): supplied argument is not a valid stream resource in line 12
I am a lucky girl.
Warning: fwrite(): supplied argument is not a valid stream resource in line 12

I have been searching and searching for a long time and have yet found a solution.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: How to output to screen and a file at the same time?

Post by susrisha »

1. Yes i am in india
2. Yes its far but we got enough network(internet) to reach out to world
3. Check the folder permissions of the directory where your yyyy.txt is placed.
4. change the code like this to know if the file handle has been alloted

Code: Select all

 
error_reporting(E_ALL); //to report all the errors onto the page.
 
if(!$fh = fopen($file, "a"))
{
echo "Could not open the file";
return;
}
else
{
//all the other earlier code here
}
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to output to screen and a file at the same time?

Post by requinix »

You got the arguments to fwrite backwards.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: How to output to screen and a file at the same time?

Post by susrisha »

oops..

Code: Select all

 
fwrite($fh,$string);
 
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to output to screen and a file at the same time?

Post by becky-atlanta »

Once I changed that, it was working great. I was wondering about that when I saw your example. This small test made me understand function a little bit more so it is a huge progress for me. Function always seems a mystery to me that I did not even try. But I will make more baby steps toward that. I can see how efficient it helps in coding.

I appreciate your help.

P.S. I visited your site. Did you created that? Nice site!
User avatar
Angelisa
Forum Newbie
Posts: 2
Joined: Wed Mar 04, 2009 1:36 pm
Location: Canada

Re: How to output to screen and a file at the same time?

Post by Angelisa »

I hope it is okay if I add a question to this... I did not want to start a new topic for the same sort of issue. (Can you tell I"m new here?).
Anyway I have this php component I am trying to incorporate into a website for a friend. I have had the code for a few years from friend but never tested it. Now it won't work and I can't reach the friend who wrote it.

Code: Select all

<?php
 session_start();
 if($_SESSION["UserType"] != "administrator")
 {
  header("Location: login.php");
    exit;
 }
 
 $myFile = "msg.html";
 $fh = fopen($myFile, 'w') or die("can't open file");
  
 $newContent = str_replace("\n","<br>",$_POST["message"]);
 fwrite($fh, $newContent);
 fclose($fh);
 
 $address = "edtMsg.php";
 header("Location: $address");
 exit;
?>
There are more pages (login, addmsg, edtMsg, etc etc. ) but this is the one I believe the problem is on because every time I try to save I get 'can't open file' I have insured that the directory has write priviliges. Any ideas?

Lisa
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to output to screen and a file at the same time?

Post by becky-atlanta »

Lisa,

Can you give the exact error message?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: How to output to screen and a file at the same time?

Post by susrisha »

Code: Select all

 
error_reporting(E_ALL); //to report all the errors onto the page.
 
Try to put it at the start of the file.
It will output all the errors pertaining to the script. see if anything turns up other than 'cant open file'
User avatar
Angelisa
Forum Newbie
Posts: 2
Joined: Wed Mar 04, 2009 1:36 pm
Location: Canada

Re: How to output to screen and a file at the same time?

Post by Angelisa »

Thanks,
I tried this and still all I get is 'can't open file'
I'm not sure what else to try; any ideas?
Post Reply