文件上传,传输和存储

时间:2014-02-16 18:39:03

标签: php mysql email file-io

我正在用PHP创建一个简单的邮件系统。我陷入了这种境地。我想添加一个功能,我的系统也可以上传和传输文件。但我不知道如何将文件添加到mysql数据库中。任何提示将不胜感激

compose.php

<form class="form-horizontal" method="post" action="" id="someForm" enctype="multipart/form-data">
<fieldset>

<!-- Form Name -->
<legend>Compose Mail</legend>

<!-- Text input-->


<!-- Text input-->
<div class="control-group">
  <label class="control-label" for="sendto">Send To: </label>
  <div class="controls">
    <input id="sendto" name="sendto" placeholder="Send To" class="input-xlarge cmps" type="text">
  </div>
</div>

<!-- Text input-->
<div class="control-group">
  <label class="control-label" for="title">Title: </label>
  <div class="controls">
    <input id="title" name="title" placeholder="Title/Subject" class="input-xlarge cmps" type="text">
  </div>
</div>

<!-- Textarea -->
<div class="control-group">
  <label class="control-label" for="body">Message: </label>
  <div class="controls">                     
    <textarea id="body" name="body" class="cmps"></textarea>
  </div>
</div>

<div class="control-group">
  <label class="control-label" for="file">File: </label>
  <div class="controls">
    <input id="file" name="file" type="file">
  </div>
</div>

<button type="submit" id="button" class="btn btn-success" onclick="askForSubmit()">Send!</button>
<button type="submit" id="button" class="btn-danger btn" onclick="askForSave()">Save to drafts</button>

</fieldset>
</form>

<script>
form=document.getElementById("someForm");
function askForSave() {
        form.action="http://rsantiago.mbchosting.ph/email/save.php";
        form.submit();
}
function askForSubmit() {
        form.action="http://rsantiago.mbchosting.ph/email/send.php";
        form.submit();
}


</script>

upload_file.php

<?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"] < 30000)
&& 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"] / 30000) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

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

我只想知道如何将文件存储到数据库中,以便当用户将其发送给其他用户时,可以将其下载

0 个答案:

没有答案