1

Hei, from Php oop file upload, i update the script with some function. Size check function and extension check function. But somewhere i go wrong with extension check function.

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


}

?>

And call upload is :

<?php
if(isset($_FILES['file'])){
    $fileupload = new upload();
    if(!$fileupload -> sizeck()){
        if(!$fileupload -> extens()){
            if($fileupload -> uploadfile()){
                echo 'Fisierul a fost uploadat';
            }
        }
    }   
}

?>

Where i do wrong and why??

0

1 Answer 1

1

Your if statement has !, which means it's true if your extens() returns false.

But you wan't to move on, if it's true. So remove !.

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

should be

if($fileupload -> extens()){

And you can't get extension form tmp_name. It should be name. tmp_name doesn't have extension in it's name.

So change

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

to

$this -> tmp = $_FILES["file"]["name"];
Sign up to request clarification or add additional context in comments.

2 Comments

True ...thanks, but file is not check and file with php/html and other extension who is in array, are uploaded without any problem. What are wrong in my code?:(
See my edited answer. You are getting extension from wrong name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.