PHP上傳檔案DEMO

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   檔案   ar   

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><title>upload</title></head><body><!-- **********檔案上傳測試*************form加上enctype="multipart/form-data" method="post"input type改為file**********檔案上傳測試************* --><form action="upload.php" method="post" enctype="multipart/form-data">    <input type="file" name="test[]"/>    <input type="file" name="test[]"/>    <div></div>    <input type="submit" value="submit" /></form></body></html>

index.php

 

<?php// 所有的上傳檔案都緩衝在關係數組$FILES裡面,迴圈可擷取/* * 這裡支援單個檔案上傳、多個檔案上傳(非單個控制項多選檔案,是多個控制項選中多個檔案) * <input type="file" name="test"/> * <input type="file" name="test"/> * 支援 控制項名為 name="name" 和name="name[]"的多檔案上傳 * <input type="file" name="test[]"/> * <input type="file" name="test[]"/> */foreach ($_FILES as $key => $value) {    // 擷取檔案名稱    $names = $value["name"];    // 如果檔案名稱是個數組的話就是多檔案上傳    if (is_array($names)) {        for ($i = 0; $i < count($names); $i++) {            copyFile($names[$i], $value[‘tmp_name‘][$i], $value["error"][$i]);            }    } else copyFile($names, $value[‘tmp_name‘], $value["error"]);}// 檔案名稱,臨時檔案名稱,錯誤碼function copyFile($name, $tmpName, $error) {    // 錯誤碼大於0,直接返回    if ($error > 0) return;    // 如果檔案已經存在,輸入提示資訊,不儲存    if (file_exists($name)) {        echo $name . " already exists. ";    } else { // 否則正常儲存檔案        move_uploaded_file($tmpName, $name);        echo "Stored in: " . dirname(__FILE__) . ‘\\‘ . $name;    }}?>

upload.php

 

兩個檔案置於同一個檔案夾中,發布在apache htdocs檔案夾中訪問

PHP上傳檔案DEMO

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.