PHP oop上传文件并检查扩展名


php oop upload file and check extension

嘿,从Php oop文件上传,我用一些功能更新脚本。大小检查功能和扩展检查功能。但在某个地方我错了扩展检查功能。

<?php
class upload{
    public $src = "upload/";
    public $tmp;
    public $filename;
    public $typefl;
    public $uploadfile;
    public $type = array("php", "css", "js", "html", "htm", ".php");
    function __construct(){
        $this -> filename = $_FILES["file"]["name"];
        $this -> tmp = $_FILES["file"]["tmp_name"];
        $this -> uploadfile = $this -> src . basename($this -> filename);
    }
    public function sizeck(){
        if($_FILES["file"]["size"] > 50000000){
            echo "Fisier prea mare";
            return true;
        }
    }
    public function extens(){
        $this -> typefl = pathinfo($this -> tmp, PATHINFO_EXTENSION);
        if(in_array($this -> typefl, $this -> type)){
            echo "Fisier nepermis!!!";
            return false;
        }
        else{
            return true;
        }
    }

    public function uploadfile(){
        if(move_uploaded_file($this -> tmp, $this -> uploadfile)){
            return true;
        }
    }

}
?>

呼叫上传为:

<?php
if(isset($_FILES['file'])){
    $fileupload = new upload();
    if(!$fileupload -> sizeck()){
        if(!$fileupload -> extens()){
            if($fileupload -> uploadfile()){
                echo 'Fisierul a fost uploadat';
            }
        }
    }   
}
?>
我哪里做错了,为什么?

你的if语句有!,这意味着它是true如果你的extens()返回false

但如果是true,你就不会继续前进了。所以删除!

if(!$fileupload -> extens()){

应为

if($fileupload -> extens()){

不能得到tmp_name的扩展。应该是nametmp_name的名字中没有扩展名

所以改变

$this -> tmp = $_FILES["file"]["tmp_name"];

$this -> tmp = $_FILES["file"]["name"];