检查文件是否存在(如果是,则覆盖)PHP

时间:2014-05-21 12:59:42

标签: php html forms

我正在尝试创建一个允许用户将文件上传到服务器的简单表单。但是,当用户上传与服务器上已有文件同名的文件时,我收到错误...我想要它做的是文件是否存在,删除旧文件并上传新文件一。我试过了:

if (file_exists($_FILES["file"]["name"])) {
 move_uploaded_file($_FILES["file"]["name"], $temp);

但它会生成错误警告:move_uploaded_file()期望参数2为字符串,数组在.... 我是PHP的新手,已经阅读了php.net关于如何做到这一点的描述,并且仍然感到困惑 有人可以帮忙解决这个问题吗? 源代码:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "pdf", "avi", "bmp", "css", "csv", "html", "js", "json","mdb", "exe", "xls", "pptx", "xlsx", "docx", "doc", "ppt", "pub",  "wma", "wmv", "rtf", "txt", "zip" );
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "video/x-msvideo")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "text/css")
|| ($_FILES["file"]["type"] == "text/csv")
|| ($_FILES["file"]["type"] == "text/html")
|| ($_FILES["file"]["type"] == "application/javascript")
|| ($_FILES["file"]["type"] == "application/json")
|| ($_FILES["file"]["type"] == "application/x-msaccess")
|| ($_FILES["file"]["type"] == "application/x-msdownload")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.presentationml.presentation")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/vnd.ms-powerpoint")
|| ($_FILES["file"]["type"] == "application/x-mspublisher")
|| ($_FILES["file"]["type"] == "audio/x-ms-wma")
|| ($_FILES["file"]["type"] == "video/x-ms-wmv")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/rtf")
|| ($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "application/zip"))
&& ($_FILES["file"]["size"] < 20000000)
&& 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>";
        if (file_exists($_FILES["file"]["name"])) {
     move_uploaded_file($_FILES["file"]["name"], $temp);
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      $_FILES["file"]["name"]);
      echo "Stored in: ". $_FILES["file"]["name"];
    }
  }
} else {
  echo "Invalid file";
}
?>

<!--<script>
    setTimeout(function () {
        window.location.href = './.index.php'; // the redirect goes here

    }, 5000); // 5 seconds
</script>-->

3 个答案:

答案 0 :(得分:0)

你正在这里使用这个。 explode函数返回数组。这下面的行引起了问题

$temp = explode(".", $_FILES["file"]["name"]);

答案 1 :(得分:0)

move_uploaded_file()需要一个字符串作为第二个参数。但是你正在尝试使用数组。 第二个参数表示要移动的文件的目标。

答案 2 :(得分:0)

您不能将数组用于目标。您需要指定要放置文件的位置。试试这个:

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);

这会把它放在上传文件夹中。