Php oop file upload

蓝咒 提交于 2019-12-04 19:18:19
Sebo Zoltan

The problem in your code is not triggered is because you checked variable file in the post variable and you wont find it there. the corect way to do it is

if(isset($_FILES['file'])) {
    $fileupload = new upload();

    if($fileupload -> uploadfile()) {
        echo 'Fisierul a fost uploadat';
    }
}

further more your class will not work you should pass the variables to a constructor and rename upload-> startupload() to upload-> upload

For starters you are never calling startupload().

I would suggest moving that function to the constructor (a function in the class named __construct() )

Pass $_FILES to it so you can work with the input param instead of $_FILES directly.

Change the upload code to:

if(isset($_FILES['file'])){
    $fileupload = new upload();
    $fileupload->startupload();
    if($fileupload -> uploadfile()){
        echo 'Fisierul a fost uploadat';
    }
}

And change this line in you class:

$this -> uploadfile = $this -> src . basename($this -> filename);

Also this line needs updating because the case it wrong for uploadfile:

if(move_uploaded_file($this -> tmp, $this -> uploadfile)){
jirka

you should create your own contruct where will be complete content of startupload() function

look at this:

public function __contruct($string){
    $this -> filename = $_FILES[$string]["name"];
    $this -> tmp = $_FILES[$string]["tmp_name"];
    $this -> uploadfile = $this -> src . basename($this -> filename);
}

then you couldn't use something like:

$fileupload->startupload();

but you can write only:

$fileupload = new upload("file");

and little note finally: in PHP all classes must be written with capital

function get_image(){
    $type=explode(".",$_FILES['photo']['name']);
    $type=$type[count($type)-1];
    $url="./studentimage/".uniqid(rand()).".".$type;
    if(in_array($type,array('jpeg','jpg','bmp','png'))){
        if(is_uploaded_file($_FILES['photo']['tmp_name'])){
            if(move_uploaded_file($_FILES['photo']['tmp_name'],$url)){
                return $url;
            }
        }
    }
    else{
        return "sorry dose not match my extention";
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!