
|
View Full Version : fputs() prob in PHP
evo4ever 08-07-2003, 10:32 AM Hey ppl. I've made an error log function in my "mysql" class which is comprised of writing to files to report an error. The problem is that PHP is throwing an error saying that fputs is getting an argument supplied which is an invalid stream resource.
The function:
function sql_log_error($sql_code, $sql_error, $sql_errno){
$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/logs/mysql.log", "a") || die("<br/>A fatal \"fopen();\" error has occured. Please check back later.");
$sql_mail_success = true;
$sql_mail_message = "An error has occured on the localhost website.\n";
$sql_mail_message .= "Goto the mysql log file: http://localhost/logs/mysql.log";
$send = @mail("evo4ever@phpevo.fsnet.co.uk", "Localhost Script Error!", $sql_mail_message);
if(!$send) $sql_mail_success = false;
$line = "ERROR AT: '".date("d/m/y, H:i:s")."'; IN: '".strtoupper($_SERVER["PHP_SELF"])."'.\n";
$line .= "Message: ".$sql_errno.": \"".$sql_error."\"\n";
$line .= "Code: \"".$sql_code."\"\n";
$line .= "Emailed: ".$sql_mail_success."\n";
$line .= "-----------END-----------\n\n";
fputs($fp, $line) || die("<br/>A fatal \"fputs();\" error has occured. Please check back later.");
fclose($fp);
}
Help appreciated. Thanks.
ruler 08-07-2003, 11:04 AM the code looks fine with a quick rundown, except you're missing escape character ( \ ) before your quotes in your link. have you changed the mode to 777 on mysql.log?
evo4ever 08-07-2003, 11:13 AM I've already tried using the chmod() function:
chmod($_SERVER["DOCUMENT_ROOT"]."/logs/mysql.log", 0777); // tried 0755 as well, no luck.
Hmm, what missing escape character?
ruler 08-07-2003, 11:22 AM $sql_mail_message .= "Goto the mysql log file: <a href="http://localhost/logs/mysql.log" target="_blank">http://localhost/logs/mysql.log</a>";
should be
$sql_mail_message .= "Goto the mysql log file: <a href=\"http://localhost/logs/mysql.log\" target=\"_blank\">http://localhost/logs/mysql.log</a>"
also, you can't chmod in PHP. login via FTP or SSH and chmod.
evo4ever 08-07-2003, 11:30 AM That link isnt a link, vbulletin made it a link for some reason. You can't format your text in HTML when its getting sent as an email. Outlook can't parse HTML, at least im sure of it.
Does this make sence:
if(is_writable($_SERVER["DOCUMENT_ROOT"]."/logs/mysql.log")){
print "true";
}
else {
print "false";
}
...becuase it actually returns true.
PS: php.net/chmod (http://php.net/chmod)
ruler 08-07-2003, 11:35 AM ok.. what i meant you can't chmod in php, meaning, apache usually runs as user "nobody". user "nobody" can't change the file permissions for user "evo4ever". thus, you would have to change that yourself under your username.
evo4ever 08-07-2003, 11:42 AM Apache is freshly installed on my computer. I havn't touched any FTP settings. I tried opening localhost via cmd ftp and I got an error.
"ftp: connect :Unknown error number"
Any ideas? thx.
ruler 08-07-2003, 11:56 AM oh.. i didnt know this was windows. i have never used php on windows so i can't help ya, sorry
evo4ever 08-07-2003, 12:35 PM I've found something rather strange...
I created a file called test.php, with the following contents:
$fp = fopen("new.php", "a");
fputs($fp, "Hello");
I ran it and it worked! The test.php file was ran in the root folder "htdocs" and the created file was created in the htdocs folder. This error only occurs when the file is in any other folder apart from /.
Burhan 08-07-2003, 03:14 PM ( I'm assuming that you are on windows )
If your webserver is Apache
- check to make sure that $_SERVER['DOCUMENT_ROOT'] doesn't contain the c:// part. If it does, you'll have to use the relative path to your web root
If your webserver is IIS
- check to make sure that the directory you are trying to write to has write permission under IIS (you can check this from the IIS admin console, or by using the IIS snap in for mmc)
hth
Daijoubu 08-07-2003, 08:04 PM Try to add a t/b for text/binary under win32
fopen(,'ab') or fopen(,'at')
Burhan 08-08-2003, 01:25 AM I think you might already know this but ....
PHP has a built-in error logging function that does what you are trying to do. Perhaps you should give it a whirl?
http://www.php.net/manual/en/function.error-log.php
evo4ever 08-08-2003, 08:48 AM The problem is solved.
I removed the OR ( || ) operators and the die functions on:
$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/logs/mysql.log", "a") || die("<br/>A fatal \"fopen();\" error has occured. Please check back later.");
fputs($fp, $line) || die("<br/>A fatal \"fputs();\" error has occured. Please check back later.");
...Then it worked.
bitziz 08-08-2003, 03:18 PM You can just use the word or instead...
evo4ever 08-08-2003, 07:12 PM Theres no difference between the word or and the operator or. However, I read somewhere that if you use the word OR instead of the operator, then PHP will evaluate the left hand side of the or word first, then the right side, or the other way round. But who cares now, it works. I've still added some error handling on the fopen and fputs functions tho:
$fp = @fopen("file", "a");
if(!$fp) die("message");
$fputs = @fputs($fp, "content");
if(!$fp) die("message");
The above way works, and its much simpler.
rusko 08-09-2003, 02:10 AM naturally this didnt work. please have a read about operator precedence, sequence of evaluation and lvalue assignments.
($fp = fopen($_SERVER["DOCUMENT_ROOT"]."/logs/mysql.log", "a")) || die("<br/>A fatal \"fopen();\" error has occured. Please check back later.");
this is the fix.
paul
|