PHP open file and write to it in different functions?

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

PHP open file and write to it in different functions?

Post by ayu »

I wanted one function to open a file, and one to write to it. But for some reason I got "supplied argument is not a valid stream" when doing the following. Is it not possible to have them in different functions or something? Doesn't sound right =/

Code: Select all

function opfile() {

	//Open logfile for writing
	if(!isset($_SESSION['logname'])) {

		$_SESSION['logname'] = CFG_CHANNEL . date(dmy);

		if(file_exists($_SESSION['logname']) == TRUE) {
		
			$_SESSION['logfile'] = fopen($_SESSION['logname'], 'a') or die("ERROR: could not open file");
		}
		else {

			$_SESSION['logfile'] = fopen($_SESSION['logname'], 'w') or die("ERROR: could not open file");
		}
	}

}

function writelog($data) {

fwrite($_SESSION['logfile'], $data);

}

"The best place to hide a tree, is in a forest"

User avatar
uid0
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 106
Joined: 08 Jun 2008, 16:00
15
Contact:

Post by uid0 »

I guess that is because the data inside $_SESSION['logname'] is known in the opfile() function scope, the session itself is global but not its data, maybe if you try with something like:

Code: Select all

function opfile() { 

   //Open logfile for writing 
   if(!isset($_SESSION['logname'])) { 

      $_SESSION['logname'] = CFG_CHANNEL . date(dmy); 

      if(file_exists($_SESSION['logname']) == TRUE) { 
       
         $_SESSION['logfile'] = fopen($_SESSION['logname'], 'a') or die("ERROR: could not open file"); 
      } 
      else { 

         $_SESSION['logfile'] = fopen($_SESSION['logname'], 'w') or die("ERROR: could not open file"); 
      } 
   } 
   return $_SESSION['logfile'];
} 

$logfile = opfile();

function writelog($logfile, $data) { 

fwrite($logfile, $data); 

}

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

thanks =)

wouldn't making it a constant, help as well?
"The best place to hide a tree, is in a forest"

User avatar
uid0
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 106
Joined: 08 Jun 2008, 16:00
15
Contact:

Post by uid0 »

Don't think so, constants aren't suppose to change and in your case, $_SESSION['logname'] will change everytime opfile is called because you're concatenating CFG_CHANNEL with date(dmy)

CFG_CHANNEL is static data I suppose so there's no problem with that however, date isn't and you'll end up with errors about redeclaring the constant when can't be done because the use of date().

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

uid0 wrote:Don't think so, constants aren't suppose to change and in your case, $_SESSION['logname'] will change everytime opfile is called because you're concatenating CFG_CHANNEL with date(dmy)

CFG_CHANNEL is static data I suppose so there's no problem with that however, date isn't and you'll end up with errors about redeclaring the constant when can't be done because the use of date().

Well the thing is that CFG_LOGNAME will only be set once, since the date() is used to set the date of the logfile being made, meaning that it will only be set once every day.

But yeah I just figured out an issue with that, since it would have to change every day if the bot is on 24/7, it would have to be dynamic.

Thanks for the answer =)

EDIT: I made everything into one function instead, works fine ^^
"The best place to hide a tree, is in a forest"

User avatar
uid0
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 106
Joined: 08 Jun 2008, 16:00
15
Contact:

Post by uid0 »

Congratz then cats ^^

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

uid0 wrote:Congratz then cats ^^
why thank you uid0 ^^
"The best place to hide a tree, is in a forest"

Post Reply