如果文件上载为空,则为默认文件

时间:2014-05-10 01:29:34

标签: php file-upload image-upload

我有代码将图片上传到我的网站上,我只是想知道如何制作,所以我可以将其设置为' default.png'如果用户没有选择文件。

这是表格

的代码摘录
<label for="file">Blog Post Image:</label>
<input type="file" name="file" id="file">

这是上传文件的PHP代码

    $allowedExts = array("gif", "jpeg", "jpg", "png");
$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"]["size"] < 40000)
&& 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("images/blog/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../images/blog/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../images/blog/" . $_FILES["file"]["name"];
    }
  }
} else {
  echo "Invalid file";
}

然后在这之后才被添加到数据库......

1 个答案:

答案 0 :(得分:0)

在images / blob /中保持default.png。使用is_uploaded_file()检查上传是否为空。

if(!file_exists($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
    echo 'No upload';
    // use images/blog/default.png
    echo "Upload: default.png <br>";
    echo "Type: image/png <br>";
    echo "Size: " . filesize("images/blog/default.png") . " bytes<br>";
    echo "Temp file: images/blog/default.png <br>";
} else{
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $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"]["size"] < 40000)
    && 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("images/blog/" . $_FILES["file"]["name"])) {
          echo $_FILES["file"]["name"] . " already exists. ";
        } else {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "../images/blog/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "../images/blog/" . $_FILES["file"]["name"];
        }
      }
    } else {
      echo "Invalid file";
    }
}