Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 10:22 AM
Randy Jackson
Guest
 
Posts: n/a
Default Problem Writing To File

Hello all.

Okay, this seems really stupid, but it's driving me up the wall.

I have a simple script I've written to log some information to a text
file. Everything seems to be okay, the code isn't throwing any errors,
but for some reason it isn't writing to file. If anyone has any ideas,
please let me know.

Here's the code I'm using:

//trigger_error("Just Testing to see if this is working",E_USER_NOTICE);
$strTimeStamp=date("F j, Y h:i:s A");
$strLogEntry=$strTimeStamp . ", " . $REQUEST_URI . ", " . $_SERVER
['HTTP_REFERER']. "\r\n";
//echo $strLogEntry;
$logfile='../logs/fce.log';

$logExists=file_exists($logfile);
if($logExists){
echo "File Exists<br>";
}
else{
echo "File Does Not Exist<br>";
}

$writable=is_writable($logfile);
if($writable){
echo "File Can Be Written to<br>";
}
else{
echo "File Cannot Be Written to<br>";
}

$fileHandle=fopen($logfile, "a");



if($fileHandle){
echo "File Opened for writing.<br>";
$written=fwrite($fileHandle, $strLogEntry);
echo "File written to successfully.<br>";
$closed=fclose($fileHandle);
echo "File closed succesfully.<br>";

}
else{
error_log("$logfile does not exist");
}



--
Randy Jackson
http://fourcolorexplosion.com
  #2  
Old July 17th, 2005, 10:22 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: Problem Writing To File

["Followup-To:" header set to comp.lang.php.]
Randy Jackson wrote:[color=blue]
> I have a simple script I've written to log some information to a text
> file. Everything seems to be okay, the code isn't throwing any errors,
> but for some reason it isn't writing to file. If anyone has any ideas,
> please let me know.[/color]

Turn on error reporting if you don't already have it.

Put these at the top of your scripts
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>

or change php.ini to have it on at all times.
[color=blue]
> Here's the code I'm using:
>
> //trigger_error("Just Testing to see if this is working",E_USER_NOTICE);
> $strTimeStamp=date("F j, Y h:i:s A");
> $strLogEntry=$strTimeStamp . ", " . $REQUEST_URI . ", " . $_SERVER[/color]
^^^^^^^^^^^^
Not that it matters for the problem at hand,
but do you have "register globals" on?
[color=blue]
> ['HTTP_REFERER']. "\r\n";[/color]
^^^^^^
Shouldn't this be only "\n" and let the OS do the translation?
Again, it doesn't matter for your problem :-)

[snip][color=blue]
> $fileHandle=fopen($logfile, "a");[/color]
^^^
text-mode translation; use "ab" for binary-mode.


[color=blue]
> if($fileHandle){
> echo "File Opened for writing.<br>";
> $written=fwrite($fileHandle, $strLogEntry);[/color]

if ($written === false) echo "ERROR: Could not write to file.<br>";
else {
echo "wrote $written bytes to file -- expected to write ";
echo strlen($strLogEntry), ".<br>";
}
[color=blue]
> echo "File written to successfully.<br>";
> $closed=fclose($fileHandle);
> echo "File closed succesfully.<br>";
>
> }
> else{
> error_log("$logfile does not exist");
> }[/color]

Happy Bug Hunting :-)
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
  #3  
Old July 17th, 2005, 10:22 AM
Steven Stern
Guest
 
Posts: n/a
Default Re: Problem Writing To File

On Fri, 29 Oct 2004 16:53:58 -0500 (more or less), Randy Jackson
<randyjackson@nounsolicitedfourcolorexplosion.co m> wrote:
[color=blue]
>Hello all.
>
>Okay, this seems really stupid, but it's driving me up the wall.
>
>I have a simple script I've written to log some information to a text
>file. Everything seems to be okay, the code isn't throwing any errors,
>but for some reason it isn't writing to file. If anyone has any ideas,
>please let me know.
>
>Here's the code I'm using:[/color]

Your code worked for me! I did have to add <? to the beginning and ?> to the
end, but that was it. The contents of fce.log are "October 30, 2004 09:08:13
AM, , ". Make sure you have create and write permissions in the logs
directory for the user id of your httpd daemon.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles