简体   繁体   English

无法通过PHP上传.xml文件

[英]Trouble uploading .xml file via PHP

I'm trying to create a form where a user can upload an xml file to my server which will overwrite one that already exists. 我正在尝试创建一种表单,用户可以在其中将xml文件上传到我的服务器,该文件将覆盖已经存在的XML文件。 I'm using the code below (which I know doesn't upload the file if it already exists, but I'll fix that once I've fixed this). 我正在使用下面的代码(如果文件已经存在,我不会上传该文件,但是一旦修复,我将对其进行修复)。

The problem is that the first if statement fails at this statement 问题是第一个if语句在此语句处失败

($_FILES["file"]["type"] == "application/xml")

meaning that I get an invalid file message. 表示我收到了无效的文件消息。 If I comment that out, the file uploads. 如果我将其注释掉,则将上传文件。

Changing the file types allowed to another type (eg jpg) works. 将允许的文件类型更改为其他类型(例如jpg)是可行的。

<?php
$allowedExts = array("xml");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/xml"))
&& ($_FILES["file"]["size"] < 5000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("xml/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "xml/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "xml/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

I'm very new to PHP so be gentle, I have looked for similar questions but not found anything that seems to fix this issue. 我对PHP还是很陌生,所以要保持谦虚,我一直在寻找类似的问题,但没有发现任何可以解决此问题的方法。

Thanks. 谢谢。

From PHP website: 从PHP网站:

$_FILES['userfile']['type'] $ _FILES ['userfile'] ['type']

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

"if the browser provided this information." “如果浏览器提供了此信息。” some browser don't send type. 一些浏览器不发送类型。 You need validate via extension or parse XML. 您需要通过扩展名进行验证或解析XML。

Check this. 检查一下。

与它一起工作

($_FILES["file"]["type"] == "text/xml")

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

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