【发布时间】:2018-08-30 09:46:36
【问题描述】:
我正在构建一个文件上传表单,用户可以在其中提交 XML 配置文件。我的目标是获取全部内容并将它们作为新帖子(自定义帖子类型)插入 Wordpress。它大部分都在工作,除了我在使用file_get_contents() 时很难将 XML 标记与内容一起提取。这在使用时会省略 XML 标记吗?如果是这样,我怎样才能得到文件的全部内容?
这是我的表单和处理程序代码...
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);
}
}
【问题讨论】:
-
在下面查看我的答案。
-
内容没有被省略,因为您只是将其输出到浏览器,浏览器试图将标签理解为 HTML。如果您查看页面的源代码 - 您可能会在其中看到 XML 标记。
-
很高兴知道,非常感谢!
标签: php wordpress forms file-upload file-get-contents