Search code examples
phpwordpressformsfile-uploadfile-get-contents

Get full content of uploaded XML file in PHP


I'm building a file upload form where users can submit XML configuration files. My goal is to get the full contents and insert them into Wordpress as a new post (custom post type). It's working for the most part, except I am having difficulty pulling the XML tags with the content when using file_get_contents(). Does this omit XML tags when used? If so, how can I get the full contents of the file?

Here is my code for the form and handler...

function slicer_profile_form()
{
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post" enctype="multipart/form-data">';

    echo '<p>';
    echo 'Profile<br />';
    echo '<input type="file" name="slicer-profile" accept="text/xml">';
    echo '</p>';

    echo '<p><input type="submit" name="slicer-profile-submitted" value="Submit"/></p>';
    echo '</form>';
}

function slicer_profile_submit()
{
    // if the submit button is clicked, submit
    if (isset($_POST['slicer-profile-submitted']))
    {
        $contents = file_get_contents( $_FILES['slicer-profile']['tmp_name'] );
        var_dump($contents);
    }
}

Solution

  • You can use this.

     <?php
    $xml=simplexml_load_file($_FILES['slicer-profile']['tmp_name']) or die("Error: Cannot create object");
    print_r($xml);
    ?> 
    

    It will give the xml content.