/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP file write question PLEASE HELP!

s it possible to use PHP to add/remove text from a flat file?

Basically, what I’m attempting to do is have users click to add/remove (manage) songs from a dynamically generated XML playlist.
I’ve used the file write command to generate a successful XML playlist file, but every time the form data is submitted, it overwrites the entire file.
I want the user to be able to not only add songs, but remove old ones as well.

I’ve considered simply adding or removing songs from a database table, but how then would I generate the playlist from database information?

PLEASE HELP!

Please send help to: [email][email protected][/email]

Thank you!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@stespeakdesignsJul 23.2007 — Never really dealt with file writing or XML properly in PHP but one "hack fix" I would suggest is, if you could define the contents of the file to a variable then use the .= to add to the end of the variable the new data, then overwrite the file with the content from the new variable. Meaning it will contain the old code plus the extra code at the end.

Does that make sense?
Copy linkTweet thisAlerts:
@robmarstonauthorJul 23.2007 — I got the "fwrite" command to write to the file using "w" for "write" - I can show you the code, or you can take a look at the test at:

http://www.yourownsound.com/StationYou/test.html

I understand I can use "a" to append or add another block of text (or song) to the playlist.

The problem however is removing a line that has previously been added.

If I define a variable in the XML file, it'll cause the file to not read correctly by the video player (or at least I'd assume so?)

Thanks.
Copy linkTweet thisAlerts:
@robmarstonauthorAug 15.2012 — Marking as resolved because it's outdated. Can use the following code:

[code=php]
$input = file_get_contents("filename.txt");
// modify $input using regular expression, e.g. preg_replace
file_put_contents($input);
[/code]
×

Success!

Help @robmarston spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...