php - PHP 文件上传的 Mime 验证不起作用

标签 php html mysql oop

我试图在将文件上传到数据库之前验证文件的 MIME 类型。但是,我的程序没有得到任何输出。谁能帮我解决这个问题?提前致谢:)

表单句柄代码(handleUpload.php)

<?php
    if (isset($_POST['submit'])) {
        $Upload = new Upload();
        if (function_exists("check_doc_mime")) {
            //validate MIME type
            $validateMime = $Upload->check_doc_mime($_FILES['filename']['tmp_name']);
           if (!$Upload->check_doc_mime($validateMime)) {
                /* Not a MIME type we want on our site, stop here
                 * and return an error message, or just die(); */
                echo "Mime not what we want.";
            } else {
                echo "This is okay";
            }
        } 
    }
?>

函数和数据库操作代码(upload.php)

<?php
    // If it's going to need the database, then it's 
    // probably smart to require it before we start.
    require_once(LIB_PATH . DS . 'database.php');

    class Upload extends DatabaseObject {

        protected static $table_name = "resume";
        protected static $db_fields = array('resume_id', 'individual_id', 'resume_title', 'file_type', 'file_size', 'upload_date', 'status', 'resume_data');
        public $resume_id;
        public $individual_id;
        public $resume_title;
        public $file_type;
        public $file_size;
        public $upload_date;
        public $status;
        public $resume_data; 
        protected $destination; //so cannot be changed outside of class

        function check_doc_mime($tmp_name) {
          // MIME types: http://filext.com/faq/office_mime_types.php
          $finfo = finfo_open(FILEINFO_MIME_TYPE);
          $mtype = finfo_file($finfo, $tmp_name);
          if($mtype == ("application/vnd.openxmlformats-officedocument.wordprocessingml.document") || 
            $mtype == ("application/vnd.ms-excel") ||
            $mtype == ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") || 
            $mtype == ("application/vnd.ms-powerpoint") ||
            $mtype == ("application/vnd.openxmlformats-officedocument.presentationml.presentation") || 
            $mtype == ("application/pdf")) {
            return TRUE;
          }
          else {
            return FALSE;
          }
          finfo_close($finfo);
        }



        public function uploadResume($fileName, $tmpName, $fileSize, $fileType, $date){

            global $database;

            $fp = fopen($tmpName, 'r');
            $content = fread($fp, filesize($tmpName));
            $content = addslashes($content);
            fclose($fp);

            if(!get_magic_quotes_gpc())
            {
            $fileName = addslashes($fileName);
            }
             $sql = "INSERT INTO resume (resume_title, file_size, file_type, resume_data, status, individual_id) ".
                "VALUES ('$title', '$fileSize', '$fileType', '$content', '1', '$id')";
             $database->query($sql);
        }



    }

    $Upload = new Upload();
    $upload =& $Upload;
?>

最佳答案

原来是因为我没有正确编写 if 语句。它应该是 if (!$validateMime) { 而不是 if (!$Upload->check_doc_mime($validateMime)) {

关于php - PHP 文件上传的 Mime 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31666652/

相关文章:

javascript - HTML5 表单必需属性。如何检查所需的消息是否已显示?

php - CakePHP:根据配置值全局更改所有模型的数据库连接

php - 无法在 Laravel 中检索获取变量

php - 文件(图像)的自动完成搜索框;有可能吗,怎么办?

javascript - jquery 隐藏/显示来自 ul 的列表元素

android - 如何将联系人姓名和电话号码发送到MySQL数据库?

php - 重复的、自动生成的 HTML 元素 : Inject with JS or PHP

php - 为什么我应该使用类而不是函数的集合?

php - 如何在mysql查询中使用varchar类型的大于运算符

mysql - SQL查询,显示属性最多的人