PHP write to text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

PHP write to text file

Post by chris98 »

Hello, I am currently working on a website, and I want to create an uploads section.I have got this working, however now I would like to put the Users Username who has uploaded something into a text file in the same location as the upload.I have tried for ages, and was wondering if anyone knows how?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP write to text file

Post by requinix »

Any reason you're not using a database? That's the best place for that kind of information.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP write to text file

Post by Christopher »

Code: Select all

$file = '/path/to/some/file.txt';
$username = 'foobar';
file_put_contents($file, $username);
You need write access to that directory, which I assume you have since you are uploading files to it.
(#10850)
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: PHP write to text file

Post by chris98 »

Thanks, I can get it to work now!
Post Reply