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

时间:2015-07-16 10:23:55

标签: php oop

Hei,来自Php oop file upload,我用一些函数更新脚本。尺寸检查功能和扩展检查功能。但在某些地方我出现了扩展检查功能。

<?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';
            }
        }
    }   
}

?>

我做错了,为什么?

1 个答案:

答案 0 :(得分:1)

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"];