代码之家  ›  专栏  ›  技术社区  ›  Fussionweb

在docx文件中上载错误,但能够在相同代码中上载pdf

  •  0
  • Fussionweb  · 技术社区  · 7 年前

    Am试图上传docx文件格式,xlsx它没有将文件移动到它抛出错误的位置。 这里是上传代码

    /*     Upload Pdf  */
        if(isset($_FILES["file"]) && $_FILES["file"]["error"] == 0){
        $allowed = array('pdf' => "application/pdf",'doc' => "application/msword",'docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        $filename = $_FILES["file"]["name"];
        $filetype = $_FILES["file"]["type"];
        $filesize = $_FILES["file"]["size"];
    
        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Verify file size - 5MB maximum
        $maxsize = 5 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than 5MB the allowed limit.");
    
        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("../../upload/courses/pdf/" . $_FILES["file"]["name"])){
                unlink("../../upload/courses/pdf/" . $_FILES["file"]["name"]);
    
                $tmph = $_FILES["file"]["tmp_name"];
                $pathh = "../../upload/courses/pdf/" . $_FILES["file"]["name"];
    
                move_uploaded_file($tmph,$pathh );
            } else{
                $tmph = $_FILES["file"]["tmp_name"];
                $pathh = "../../upload/courses/pdf/" . $_FILES["file"]["name"];
    
                move_uploaded_file($tmph,$pathh );
                //echo "Your file was uploaded successfully.";
    
            } 
        } else{   echo "Error: There was a problem uploading your file. Please try again.";        }
    
    
    
    
    } 
            $filepath =substr("../../upload/courses/pdf/" . $_FILES["file"]["name"],3);
    
           if($_FILES["file"]["size"] == 0 && $_FILES["file"]["error"] == 4){
    
            $file ='';
            }
            else{
                $file = ",file='".$filepath."'";
            }
    

    链接move_uploaded_file()部分出错

    通过使用相同的文件代码,我可以上传pdf文件,但造成错误的docx和xlsx文件格式。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Fussionweb    7 年前

    在上面的部分,我检查了MIME和打印文件类型,它给我的文件mimetype我需要使用。 所以我改变了

    $allowed = array('pdf' => "application/pdf",'doc' => "application/msword",'docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    

    $allowed = array('pdf' => "application/pdf",'doc' => "application/msword",'docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document','docx'=>'application/octet-stream');
    

    在我的案子里,这场比赛很成功 'docx'=>'application/octet-stream' ,以防错误意味着尝试检查 $_FILES["file"]["type"]; 通过回声或打印。

    推荐文章