程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php文件上傳及錯誤代碼總結

php文件上傳及錯誤代碼總結

編輯:關於PHP編程

本文章總結了關於php文件上傳及錯誤代碼的介紹,有需要學習的朋友可參考一下本文章。

上傳操作代碼

 代碼如下 復制代碼

<!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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved