PHP 檔案上傳

來源:互聯網
上載者:User

標籤:font   form   上傳   伺服器   html   data   dex   目錄   檔案名稱   

通過 PHP,可以把檔案上傳到伺服器。
test 項目下完成,目錄結構為:

test
|-----upload             # 檔案上傳的目錄
|-----form.html          # 表單檔案
|-----upload_file.php    # php 上傳代碼


form.html  # 表單檔案

<html><head><meta charset="utf-8"><title>菜鳥教程(runoob.com)</title></head><body><form action="upload_file.php" method="post" enctype="multipart/form-data">    <label for="file">檔案名稱:</label>    <input type="file" name="file" id="file"><br>    <input type="submit" name="submit" value="提交"></form></body></html><form>

 

upload_file.php    # php 上傳代碼

<?php// 允許上傳的圖片尾碼$allowedExts = array("gif", "jpeg", "jpg", "png");$temp = explode(".", $_FILES["file"]["name"]);echo $_FILES["file"]["size"]. "<br>";$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"] < 204800)   // 小於 200 kb&& in_array($extension, $allowedExts)){    if ($_FILES["file"]["error"] > 0)    {        echo "錯誤:: " . $_FILES["file"]["error"] . "<br>";    }    else    {        echo "上傳檔案名稱: " . $_FILES["file"]["name"] . "<br>";        echo "檔案類型: " . $_FILES["file"]["type"] . "<br>";        echo "檔案大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";        echo "檔案臨時儲存的位置: " . $_FILES["file"]["tmp_name"] . "<br>";                // 判斷當期目錄下的 upload 目錄是否存在該檔案        // 如果沒有 upload 目錄,你需要建立它,upload 目錄許可權為 777        if (file_exists("upload/" . $_FILES["file"]["name"]))        {            echo $_FILES["file"]["name"] . " 檔案已經存在。 ";        }        else        {            // 如果 upload 目錄不存在該檔案,則將檔案上傳到 upload 目錄下            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);            echo "檔案儲存體在: " . "upload/" . $_FILES["file"]["name"];        }    }}else{    echo "非法的檔案格式";}?>

 

PHP 檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.