Page 1 of 1

Write text to a different folder

Posted: Sat Jun 11, 2005 1:18 pm
by kennybunkport
I looked all over this site and nobody seemed to need this information.

I have one folder that has open write permissions. It's the only folder I want open to writing.

But, I have a number of other pages in other folders that I would like to make a log for.

So i need to run the log script in one folder and write to another.

Code: Select all

<?php 
$data = "This is a new file entry!\n";   
$file = "newfile.txt";    
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }   
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }   
echo "You have successfully written data to $file";    
fclose($file_handle);   
?>
works fine if this file is in the open folder 'UserLog'

Code: Select all

<?php 
$data = "This is a new file entry!\n";   
$file = "/UserLog/newfile.txt";    
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }   
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }   
echo "You have successfully written data to $file";    
fclose($file_handle);   
?>
Does not

I also tried using the actual Path "d:\blah\blah\UserLog\write.php"
I didn't write this code BTW, I'm just learning this language.



JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]

Posted: Sun Jun 12, 2005 1:06 pm
by andylyon87
Had a look and try the extension without the / at the front

Code: Select all

<?php 
$data = "This is a new file entry!\n";   
$file = "UserLog/newfile.txt";    
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }   
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }   
echo "You have successfully written data to $file";    
fclose($file_handle);   
?>
works for me

Posted: Sun Jun 12, 2005 1:14 pm
by kennybunkport
Thank you for having a look.

It didn't work. Maybe because I'm running it on a Win machine ( maybe ) ?

The file I'm writting from is higher on the folder tree than 'UserLog' also.

Maybe I'll talk to my site admin. They usually have a good explaination and can solve problems when it LOOKS like things should be working.

Thaks again!

Posted: Sun Jun 12, 2005 2:58 pm
by andylyon87
so you are running from UserLog/some folder

in this case your root would be ../UserLog/txtfile.txt

Posted: Sun Jun 12, 2005 4:02 pm
by kennybunkport
The program will need to be run from any folder as it is a log.

example:

/file.php /* include log.php */

/foldera/file.php /* include log.php */

/folderb/folderc/file.php /* include log.php */

etc...

I would need to be pointing to the UserLog file for writing, but running the file wherever I need it.


I'm just loking for a way to log user traffic. Ideally in an html file contained in UserLog.

If there is another method I'm open to sugggestions.

THX

Posted: Mon Jun 13, 2005 3:35 am
by andylyon87
in that cse wouldnt the $file variable change all the time depending on its situation to the UserLog

would it not be better to remove $file and place it on the page of the log and then call the UserLog using include

Code: Select all

$file= "";
include("../../UserLog.php");

Posted: Mon Jun 13, 2005 9:55 am
by kennybunkport
I tried that but got the same error.
It seems that even if the file is located in a different directory.
Because it is physically running in this directory it takes on this directories characteristics.