Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9091227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:33:37+00:00 2026-06-16T22:33:37+00:00

I am using SimpleXML to write to my XML file on my Apache Server.

  • 0

I am using SimpleXML to write to my XML file on my Apache Server. Here is my PHP code:

<?php
  $xmlFile = 'http://localhost/database.xml';
  //$xml = new SimpleXMLElement($xmlFile, NULL, TRUE);
  $xml = simplexml_load_file($xmlFile);
  $xml->addChild("User", "TestUser2");
  file_put_contents($xmlFile, $xml->asXML());
?>

My XML file code:

<Usernames>
  <User>TestUser1</User>
</Usernames>

The problem I am having is that SimpleXML WILL NOT write to my XML file. I have tried many different methods ($xml->asXML($xmlFile), DOMDocument ... ->save) and none of them are working. I changed the permissions on my file and STILL I cannot write to it:

Permissions

I have spent hours today trying to get this to work with no success. If anyone has any type of solution it would be great to hear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:33:39+00:00Added an answer on June 16, 2026 at 10:33 pm

    When you write the contents to the file, you should pass a system filepath as the first variable, your $xmlFile variable is a URL. Change this to the local file name and it should save.

    Based on your comments, the following should work

    <?php
      $xmlFile = 'http://localhost/database.xml';
      $xml = simplexml_load_file($xmlFile);
      $xml->addChild("User", "TestUser2");
      file_put_contents('/Applications/MAMP/htdocs/DataBase/database.xml', $xml->asXML());
    

    But, I would double check the $xmlFile URL – from what you have said, your local URL could be http://localhost/DataBase/database.xml – you should check that you can open your XML file in Safari using the $xmlFile URL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using XML for a config file in PHP (Using SimpleXML), in creating the
I have been trying to create an XML using the simplexml library (v2.6.2) http://simple.sourceforge.net/home.php
I am using SimpleXML to load a remote XML My PHP code looks like
I am using simpleXML in PHP to fetch xml file. The thing is that
How do I convert SOAP response like this to php array using SimpleXML? <?xml
Possible Duplicate: Creating a simple XML file using python I want to write a
I use SimpleXml to write XML file to internal storage in Android device. The
I am using php to parse an XML file, but I get an error
I'm trying to parse some XML using simpleXML in PHP. The problem I'm having
I am trying to read this XML file using PHP and I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.