php檔案上傳及錯誤碼總結

來源:互聯網
上載者:User

上傳作業碼

 代碼如下 複製代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title></title>
</head>
<body>
 
<?php
    if($_FILES['myfile']['error'] > 0) {      //判斷檔案是否可以成功上傳到伺服器,0表示上傳成功
        echo 'Error: ';
 
        switch ( $_FILES['myfile']['error']  ) {
            case UPLOAD_ERR_OK:
                $response = 'There is no error, the file uploaded with success.';
                break;
            case UPLOAD_ERR_INI_SIZE:
                $response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
                break;
            case UPLOAD_ERR_FORM_SIZE:
                $response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
                break;
            case UPLOAD_ERR_PARTIAL:
                $response = 'The uploaded file was only partially uploaded.';
                break;
            case UPLOAD_ERR_NO_FILE:
                $response = 'No file was uploaded.';
                break;
            case UPLOAD_ERR_NO_TMP_DIR:
                $response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
                break;
            case UPLOAD_ERR_CANT_WRITE:
                $response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
                break;
            case UPLOAD_ERR_EXTENSION:
                $response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
                break;
            default:
                $response = 'Unknown error';
                break;
        }
        echo $response;
         
         
        exit;       //如果$_FILES['myfile']['error']大於0都是有錯誤,輸出錯誤資訊並退出程式
    }
     
     //擷取上傳檔案的MIME類型中的主類型和子類型
    list($maintype,$subtype)=explode("/",$_FILES['myfile']['type']);    
     
    if ($maintype=="text") {    //通過主類型限制不能上傳文字檔,例如.txt .html .php等檔案檔案
     
        echo '問題: 不能上傳文字檔。';
        exit;                //如果使用者上傳文字檔則退出程式
         
    }
 
    $upfile = './uploads/'.time().$_FILES['myfile']['name'];     //定義上傳後的位置和新檔案名稱
     
    if ( is_uploaded_file($_FILES['myfile']['tmp_name']) ) {     //判斷是否為上傳檔案
     
        if ( !move_uploaded_file($_FILES['myfile']['tmp_name'], $upfile) ) {   //從移動檔案
            echo '問題: 不能將檔案移動到指定目錄。';
            exit;
        }
         
    }else{
         
        echo '問題: 上傳檔案不是一個合法檔案: ';
        echo $_FILES['myfile']['name'];
        exit;
         
    }
 
    echo '檔案 :  '.$upfile.'  上傳成功, 大小為 : ' .$_FILES['myfile']['size'].'!<br>';   //如果檔案上傳成功則輸出
?>
</body>
</html>

html

 代碼如下 複製代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<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 type="hidden" name="MAX_FILE_SIZE" value="1000000">
          選擇檔案:<input type="file" name="myfile">
          <input type="submit" value="上傳檔案">
      </form>
</body>
</html>

一些常見的上傳檔案時錯誤碼

0 | UPLOAD_ERR_OK         | 檔案成功上傳
1 | UPLOAD_ERR_INI_SIZE   | Size exceeds upload_max_filesize in php.ini.
2 | UPLOAD_ERR_FORM_SIZE  | Size exceeds MAX_FILE_SIZE specified in HTML form.
3 | UPLOAD_ERR_PARTIAL    | 檔案沒有完整上傳
4 | UPLOAD_ERR_NO_FILE    | 沒有上傳檔案
5 | UPLOAD_ERROR_E        | As expliained by @Progman, removed in rev. 81792
6 | UPLOAD_ERR_NO_TMP_DIR | 找不到臨時檔案夾
7 | UPLOAD_ERR_CANT_WRITE | 磁碟不可寫
8 | UPLOAD_ERR_EXTENSION  | File upload stopped by extension.

聯繫我們

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