3

I work on a oop php file upload script. Is simple. But doesnt work. What is the problem? I learn how to use $_FILE, and how to code oop style.

Thanks.

upload.php are :

<?php
class upload{
    public $src = "./upload/";
    public $tmp;
    public $filename;
    public $type;
    public $uploadfile;

    public function startupload(){
        $this -> filename = $_FILES["file"]["name"];
        $this -> tmp = $_FILES["file"]["tmp_name"];
        $this -> uploadfile = $src . basename($this -> name);
    }
    public function uploadfile(){
        if(move_uploaded_file($this -> tmp, $this -> uploadFile)){
            return true;
        }
    }


}

?>

index.php are:

<?php 
require_once('./lib/upload.php');
?>

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

<html>
<head></head>
<body>
<form align="center" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
Select upload file: <input type="file" name="file" required="yes"  />
<input type="submit" value="Trimite" />
<p>
</form>
</body>
</html>

Where i'm wrong with my tinking?

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

There are other issues with the code, but if you are trying to learn oop you will get to those in due time... :)
0

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)){

Comments

0

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

2 Comments

Class names do not have to begin with a capital letter (think mysqli), they just have to be referenced in the same case thier declared in. However it is best practice to define class names in camel case.
Yes, by the PSR standards you even must declared classes in CamelCase ;) thank you
0
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";
    }
}

Comments

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.