[php] 最簡易的PHP上傳程式碼 單檔+多檔上傳 (絕對可以)


 

完整又簡易的PHP檔案上傳範例

Complete Upload File PHP Script


index.php
<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    選擇要上傳的檔案:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="上傳" name="submit">
</form>

</body>
</html>

upload.php
<?php
$target_dir = "upload/"; //上傳存放的路徑
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "檔案:". basename( $_FILES["fileToUpload"]["name"]). " 已經完成上傳";
} else {
    echo "無法上傳檔案";
}


將以上兩個檔案儲存後便可以執行上傳功能。

 

多檔上傳範例(測試OK)


\upload.htm

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>多檔案上載</title>
</head>

<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
  選擇檔案一:<input name="uploadfile[]" type="file">
  選擇檔案二:<input name="uploadfile[]" type="file">
  選擇檔案三:<input name="uploadfile[]" type="file">
  <input type="submit" value="送出">
</form>
</body>
</html>

\upload.php


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>多個檔案上傳處理</title>
</head>
<body>
<?php
$target_dir=""; //上傳路徑
if (!file_exists($target_dir)) {
@mkdir($target_dir);
}
$i=count($_FILES["uploadfile"]);
$uploadNum=0;
//echo '<pre>';print_r($_FILES);die('</pre>');
for ($j=0 ; $j<$i ; $j++) {
    if(!empty($_FILES["uploadfile"]["name"][$j])){
        $tmpfile=$_FILES["uploadfile"]["tmp_name"][$j];
        $file2=mb_convert_encoding($_FILES["uploadfile"]["name"][$j],"big5","utf8");
        if(move_uploaded_file($tmpfile, $target_dir.$file2)){
            echo "上傳成功<br>";
            echo "檔案名稱:".$_FILES["uploadfile"]["name"][$j]."<br>";
            echo "檔案類型:".$_FILES["uploadfile"]["type"][$j]."<br>";
            echo "檔案大小:".$_FILES["uploadfile"]["size"][$j]."<br>";
        }else{
            echo "上傳失敗!<br> ";
            echo "檔案名稱:".$_FILES["uploadfile"]["name"][$j]."<br>";
            echo "檔案類型:".$_FILES["uploadfile"]["type"][$j]."<br>";
            echo "檔案大小:".$_FILES["uploadfile"]["size"][$j]."<br>";
            echo "失敗原因:".$_FILES['uploadfile']['error'][$j]."<br>";
        }
        ++$uploadNum;
    }
}
echo '上船數量' . $uploadNum;
?>
</body>
</html>





 

歷史錯誤訊息:上傳失敗


Warning: move_uploaded_file(uploads/1.jpg): failed to open stream: No such file or directory in C:\htdocs\upload.php on line 12

Warning: move_uploaded_file(): Unable to move 'C:\tmp\php908A.tmp' to 'uploads/1.jpg' in C:\htdocs\upload.php on line 12
Sorry, there was an error uploading your file.

413 Request entity too large 解決 Nginx 錯誤

開啟 Nginx 的設定檔 nginx.conf:
# vi /etc/nginx/nginx.conf
# vi /usr/local/nginx/conf/nginx.conf

在 http, server 或 location 其中一個 block 加入 client_max_body_size 設定, 例如我想改為 20MB:
client_max_body_size 20M;

重新啟動 Nginx:
# service nginx reload

 

回上一頁

相關文章:

喜歡這篇文章嗎?立即分享

Posted : / Views: 1656
Last updated :2017-08-10