【问题标题】:Mime validation for php file upload not workingphp文件上传的Mime验证不起作用
【发布时间】:2015-10-18 10:32:13
【问题描述】:

我正在尝试在将文件上传到数据库之前验证文件的 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;
?>

【问题讨论】:

  • 我是新手,不太清楚你的意思。是这个吗? “function(check_doc_mime) {}”在我的 upload.php 中。如果不是,请问在哪里定义它?
  • @TinyGiant 询问您在哪里写出了函数check_doc_mime。在某个地方,您可以写出类似function check_doc_mime($var) { code here } 的函数。我们需要看看这个函数做了什么。
  • 没关系,完全错过了它在第二个代码块中。但它是Upload的方法。 $Upload = new $Upload(); 应该是 $Upload = new Upload();if (!check_doc_mime($validateUser)) { 应该是 if (!$Upload-&gt;check_doc_mime($validateUser)) {
  • 我已经添加了这些更改,但我仍然没有看到我的回声。有什么我可以做的吗?

标签: php html mysql oop


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2017-11-17
    • 2014-12-24
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多