Page 1 of 2
How to output to screen and a file at the same time?
Posted: Sun Mar 01, 2009 10:59 pm
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?
Re: How to output to screen and a file at the same time?
Posted: Sun Mar 01, 2009 11:54 pm
by requinix
No.
So... any reason why you can't use those?
Re: How to output to screen and a file at the same time?
Posted: Mon Mar 02, 2009 12:00 am
by Benjamin
Code: Select all
function echoWrite($string, $file) {
echo $string;
$fh = fopen($file, ...
}
Re: How to output to screen and a file at the same time?
Posted: Mon Mar 02, 2009 6:08 am
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!
Re: How to output to screen and a file at the same time?
Posted: Tue Mar 03, 2009 9:44 pm
by becky-atlanta
Can you explain your function in more details?
Re: How to output to screen and a file at the same time?
Posted: Tue Mar 03, 2009 10:13 pm
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
}
Re: How to output to screen and a file at the same time?
Posted: Tue Mar 03, 2009 10:50 pm
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.
Re: How to output to screen and a file at the same time?
Posted: Tue Mar 03, 2009 11:07 pm
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
}
Re: How to output to screen and a file at the same time?
Posted: Wed Mar 04, 2009 12:04 am
by requinix
You got the arguments to fwrite backwards.
Re: How to output to screen and a file at the same time?
Posted: Wed Mar 04, 2009 2:36 am
by susrisha
Re: How to output to screen and a file at the same time?
Posted: Wed Mar 04, 2009 6:45 am
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!
Re: How to output to screen and a file at the same time?
Posted: Wed Mar 04, 2009 1:49 pm
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
Re: How to output to screen and a file at the same time?
Posted: Thu Mar 05, 2009 2:00 am
by becky-atlanta
Lisa,
Can you give the exact error message?
Re: How to output to screen and a file at the same time?
Posted: Thu Mar 05, 2009 2:09 am
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'
Re: How to output to screen and a file at the same time?
Posted: Wed Mar 11, 2009 8:39 pm
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?