简体   繁体   English

获取PHP中上传的XML文件的完整内容

[英]Get full content of uploaded XML file in PHP

I'm building a file upload form where users can submit XML configuration files. 我正在构建一个文件上传表单,用户可以在其中提交XML配置文件。 My goal is to get the full contents and insert them into Wordpress as a new post (custom post type). 我的目标是获取完整内容并将其作为新帖子(自定义帖子类型)插入到Wordpress中。 It's working for the most part, except I am having difficulty pulling the XML tags with the content when using file_get_contents() . 它在大多数情况下都起作用,除了在使用file_get_contents()时,我很难提取带有内容的XML标签。 Does this omit XML tags when used? 使用时是否省略XML标签? 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);
    }
}

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. 它将给出xml内容。

Your script is potentionally unsecure, you would possibly need to go through a proper file upload process using php commands like: is_uploaded_file and move_uploaded_file 您的脚本可能暂时不安全,您可能需要使用php命令(例如is_uploaded_file和move_uploaded_file)进行正确的文件上传过程

I always check for blacklisted file types aswell as checking the type of file I am receiving. 我总是检查列入黑名单的文件类型以及检查我收到的文件类型。 You will also need to make sure the contents of what you are receiving are also safe. 您还需要确保所接收内容的安全性。

By changing to: 通过更改为:

echo '<pre>';
var_dump($contents);
echo '</pre>';

Your current script might output correctly. 您当前的脚本可能会正确输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM