簡體   English   中英

如何更改消息:上傳的文件超過了 php.ini 中的 upload_max_filesize 指令

[英]HOW TO CHANGE THE MESSAGE: The uploaded file exceeds the upload_max_filesize directive in php.ini

您好,每當您降低上傳文件大小然后嘗試上傳大於 php.ini 中指定大小的文件時,就會出現此通用消息我不尋求如何解決它,我想更改消息的內容,但我只是找不到在哪里。 web 充滿了如何修復它的答案:) 謝謝!

如果您只想捕獲異常並更改為“nice”消息,則需要將上傳包裝在 try catch 中並測試以下異常類型UPLOAD_ERR_INI_SIZE

從那里您可以簡單地用您喜歡的消息編輯/更改/替換消息。

值得注意的是,您可能希望處理更多錯誤消息,它們在 文檔中列出。

這是“直接”的 PHP,因此 WordPress 可能需要少量,我現在沒有方便的 WP 安裝。

try {
    //upload stuff
} catch (Exception $e) {
    switch($_FILES['file']['error']) {
        case UPLOAD_ERR_OK:
            //for completeness sake
            $message = "File Uploaded Successfully";
            break;
        case UPLOAD_ERR_INI_SIZE:
            $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
            break;
        case UPLOAD_ERR_FORM_SIZE:
            $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
            break;
        case UPLOAD_ERR_PARTIAL:
            $message = "The uploaded file was only partially uploaded";
            break;
        case UPLOAD_ERR_NO_FILE:
            $message = "No file was uploaded";
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            $message = "Missing a temporary folder";
            break;
        case UPLOAD_ERR_CANT_WRITE:
            $message = "Failed to write file to disk";
            break;
        case UPLOAD_ERR_EXTENSION:
            $message = "File upload stopped by extension";
            break;
        default:
            $message = "Unknown upload error";
            break;
    }
    //deal with returning the message here
}

只需使用您喜歡的任何自定義消息編輯$message即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM