本文參考自 http://alfredwebdesign.blogspot.tw/2014/08/php.htm... 及 http://stackoverflow.com/questions/10453735/trying...

範例如下

1.上傳的upload.html檔

<html>
    <body>
        <form action="upload_file.php" method="post" enctype="multipart/form-data">
            <label for="file">Filename:</label>
            <input type="file" name="file" id="file">
            <input type="submit" name="submit" value="Submit">
        </form>
    </body>
</html>

2.上傳的upload_file.php檔

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png","mp4");
$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"] == "video/mp4"))
&& ($_FILES["file"]["size"] < 2000000000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "
";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "
";
    echo "Type: " . $_FILES["file"]["type"] . "
";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
    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";
}
?>

如果你會上傳較大的檔案,要去修改php.ini的預設值,實際數字視你自己的需求做調整

Find: 
post_max_size = 8M 
upload_max_filesize = 2M 
max_execution_time = 30 
max_input_time = 60 
memory_limit = 8M
Change to: 
post_max_size = 750M 
upload_max_filesize = 750M 
max_execution_time = 5000 
max_input_time = 5000 
memory_limit = 1000M

arrow
arrow
    文章標籤
    php upload example
    全站熱搜

    痞客興 發表在 痞客邦 留言(0) 人氣()