473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Jul 17 '05 #1
2 2190
["Followup-To:" header set to comp.lang.php.]
Randy Jackson wrote:
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.
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.
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 ^^^^^^^^^^^^
Not that it matters for the problem at hand,
but do you have "register globals" on?
['HTTP_REFERER']. "\r\n"; ^^^^^^
Shouldn't this be only "\n" and let the OS do the translation?
Again, it doesn't matter for your problem :-)

[snip] $fileHandle=fopen($logfile, "a"); ^^^
text-mode translation; use "ab" for binary-mode.
if($fileHandle){
echo "File Opened for writing.<br>";
$written=fwrite($fileHandle, $strLogEntry);
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>";
}
echo "File written to successfully.<br>";
$closed=fclose($fileHandle);
echo "File closed succesfully.<br>";

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


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. |
Jul 17 '05 #2
On Fri, 29 Oct 2004 16:53:58 -0500 (more or less), Randy Jackson
<ra**********@nounsolicitedfourcolorexplosion.co m> wrote:
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:


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.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pierre Rouleau | last post by:
Hi all! I'm trying to extend the functionality of the file object by creating a class that derives from file. MyFile class re-implements __init__(), write(), writelines() and close() to augment...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
by: deacon57 | last post by:
FYI - If you are a computer scientist (or geek), this post may be for you. I wanted to log any search keywords, etc that are used on my site. I created a simple program that logs search terms...
2
by: Thelonious Monk | last post by:
I have a problem where some data is being eliminated. The problem is that the data contains signed numeric fields (the low-order byte of a negative number uses the first 4 bits as a sign and...
10
by: Rahul | last post by:
Friends, My Problem is related to writing a text file from a database using .Net. in a particular string format. I have a dataset with a table, let suppose in this table there are three fields....
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.