CodeIgniter Forums
File helper... not helping - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: File helper... not helping (/thread-23257.html)



File helper... not helping - El Forum - 10-05-2009

[eluser]richzilla[/eluser]
Hi all, im having a little bit of a problem getting to grips with the file helper. Im trying to get it to write to a log file so i can see when users have logged into my application, made changes etc etc. However, although i seem to have loaded everything correctly, it is making no effort to write to the file. Ive checked file an folder permissions all the way back to my root folder and there all good, but when i use the get_filenames method, it cant find it either.

My code is:

i set the data to be written here:

Code:
$this->write_log(unix_to_human(now()).': USER: '.$this->session->userdata('userid').' Logged In');

and the method being called

Code:
function write_log($data)
    {
        write_file(base_url().'assets/log/main_log.txt',$data);
    }

i appreciate i could be missing something simple, so if anyone can point me in the correct direction, i would appreciate it.

Cheers


File helper... not helping - El Forum - 10-05-2009

[eluser]Phil Sturgeon[/eluser]
You cannot write to a URL. Instead you must write to a path.

Code:
function write_log($data)
    {
        write_file(APPPATH.'assets/log/main_log.txt',$data);
    }



File helper... not helping - El Forum - 10-05-2009

[eluser]richzilla[/eluser]
Fantastic, worked a treat. How would i get it to add a new line after every entry? at present its just stacking everything up together. Ive tried adding '/n' to the end of the $data variable but still no luck.

Cheers


File helper... not helping - El Forum - 10-05-2009

[eluser]bretticus[/eluser]
[quote author="ricardino" date="1254774320"]How would i get it to add a new line after every entry? at present its just stacking everything up together. Ive tried adding '/n' to the end of the $data variable but still no luck.[/quote]
Code:
$string = "My new line\n";
write_file('./path/to/file.php', $string, 'a');



File helper... not helping - El Forum - 10-05-2009

[eluser]BrianDHall[/eluser]
Addition to bretticus's tip - be sure to use double-quoted strings! It won't work if you use single quotes.


File helper... not helping - El Forum - 10-05-2009

[eluser]John_Betong[/eluser]
 
>>> i appreciate i could be missing something simple, so if anyone can point me in the correct direction, i would appreciate it.
 
For a very similar problem I have just used CI's Session Class and "Saving Session Data to a Database".
 
Many thanks to a clear and descriptive previous post from BrianDHall - standing on the shoulders' of giants springs to mind Smile