.mp3 Filetype Upload

后端 未结 6 1405
清歌不尽
清歌不尽 2021-01-07 06:31

I\'m working on a PHP upload script which allows .mp3 file uploads amongst others. I\'ve created an array which specifies permitted filetypes, including mp3s, and set a maxi

6条回答
  •  醉梦人生
    2021-01-07 07:13

    Here is some code that will give you some symbolic meaning to your errors:

    class UploadException extends Exception { 
        public function __construct($code) { 
            $message = $this->codeToMessage($code); 
            parent::__construct($message, $code); 
        } 
    
        private function codeToMessage($code) { 
            switch ($code) { 
                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; 
            } 
            return $message; 
        } 
    } 
    
    // Use 
    if ($_FILES['file']['error'] === UPLOAD_ERR_OK) { 
        //uploading successfully done 
    } else { 
        throw new UploadException($_FILES['file']['error']); 
    }
    

    If you're getting an error from your last else statement, it is difficult to tell what exactly triggered it. Try using something like the above. http://www.php.net/manual/en/features.file-upload.errors.php

提交回复
热议问题