首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 檔案上傳完整範例(含多檔、驗證、錯誤處理)

     編輯:PHP 綜合知識

1. 上傳表單 (upload.html)

<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="max_file_size" value="100000">
<input name="userfile" type="file">
<input type="submit" value="上傳檔案">
</form>

重點:

  • enctype="multipart/form-data" 必須指定,否則伺服器無法識別檔案上傳。
  • MAX_FILE_SIZE 隱藏欄位僅為瀏覽器建議限制,可被繞過,真正限制需設定 PHP upload_max_filesize。

2. PHP 處理上傳核心參數

$f = $_FILES['Myfile'];
$dest_dir = 'uploads';
$dest = $dest_dir . '/' . date("ymd") . "_" . $f['name'];
$r = move_uploaded_file($f['tmp_name'], $dest);
chmod($dest, 0755);

3. $_FILES 陣列結構

鍵名說明
name用戶端原始檔名
typeMIME 類型(需瀏覽器支援)
size檔案大小(位元組)
tmp_name伺服器暫存檔名
error錯誤代碼(0=成功, 1=超過 php.ini 上限, 2=超過表單 MAX_FILE_SIZE, 3=部分上傳, 4=無檔案)

4. PHP 相關設定參數

  • file_uploads:是否允許 HTTP 上傳,預設 On。
  • upload_tmp_dir:暫存目錄,需寫入權限。
  • upload_max_filesize:最大上傳大小,預設 2M。

5. 完整多檔上傳範例(含驗證、重命名、錯誤訊息)

<?php
define('MUILTI_FILE_UPLOAD', 10);
define('MAX_SIZE_FILE_UPLOAD', 500000);
define('FILE_UPLOAD_DIR', 'd:/');
$array_extention_interdite = array('.php','.php3','.php4','.exe','.msi','.htaccess','.gz');

function func_message($message='', $ok='') {
    echo '<table width="100%" cellspacing="0" cellpadding="5">';
    if($ok === true) echo '<tr bgcolor="#99FF99"><td width="100"> </td><td class="text">'.$message.'</td></tr>';
    if($ok === false) echo '<tr bgcolor="#FF99CC"><td width="100"> </td><td class="text">'.$message.'</td></tr>';
    echo '</table>';
}

$action = isset($_POST['action']) ? $_POST['action'] : '';
$file = isset($_POST['file']) ? $_POST['file'] : '';
if($file != '') $file .= '/';

switch($action) {
    case 'upload':
        chmod(FILE_UPLOAD_DIR, 0777);
        for($nb=1; $nb<=MUILTI_FILE_UPLOAD; $nb++) {
            if($_FILES['file_'.$nb]['size'] >= 10) {
                if($_FILES['file_'.$nb]['size'] <= MAX_SIZE_FILE_UPLOAD) {
                    $ext = strrchr($_FILES['file_'.$nb]['name'], '.');
                    if(!in_array($ext, $array_extention_interdite)) {
                        $file_name_final = $_POST['file_name_'.$nb] ? $_POST['file_name_'.$nb].$ext : $_FILES['file_'.$nb]['name'];
                        // 檔名轉換(全形轉半形、特殊字元替換)
                        $file_name_final = strtr($file_name_final, 'aaaaaa', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
                        $file_name_final = preg_replace('/([^.a-z0-9]+)/i', '_', $file_name_final);
                        $_FILES['file_'.$nb]['name'] = $file_name_final;
                        move_uploaded_file($_FILES['file_'.$nb]['tmp_name'], FILE_UPLOAD_DIR.$file.$file_name_final);
                        $message_true .= '已上傳檔案: '.$_FILES['file_'.$nb]['name'].'<br>';
                    } else $message_false .= '副檔名不允許: '.$_FILES['file_'.$nb]['name'].'<br>';
                } else $message_false .= '檔案過大: '.$_FILES['file_'.$nb]['name'].'<br>';
            }
        }
        break;
}
?>
<html><head><title>多檔上傳</title><style>...</style></head><body>
<form name="form" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="action" value="upload">
<table>...</table>
<?php for($nb=1;$nb<=MUILTI_FILE_UPLOAD;$nb++) { ?>
<tr><td>上傳檔案:<?php echo $nb; ?></td><td><input type="file" name="file_<?php echo $nb; ?>"></td>
<td>新檔名:<?php echo $nb; ?></td><td><input type="text" name="file_name_<?php echo $nb; ?>"></td></tr>
<?php } ?>
<tr><td colspan="2">上傳目錄:<?php echo FILE_UPLOAD_DIR; ?>
<select name="file"><option value=""></option>
<?php $repertoire=opendir(FILE_UPLOAD_DIR); while($file=readdir($repertoire)) { $file=str_replace('.','',$file); if(is_dir($file)) { ?><option value="<?php echo $file; ?>"><?php echo $file; ?>/</option><?php } } closedir($repertoire); ?></select></td>
<td colspan="2"><input type="submit" value="可同時上傳<?php echo MUILTI_FILE_UPLOAD; ?> 個檔案"></td></tr></table></form></body></html>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved