Lars-Janssen's avatar

OOP Upload class

Hello,

I'm trying to make a class for uploading files that I can use multiple times. But I don't know if I'm doing everything right. Could you please give me some tips or advise.

This is my class right now: (I haven't done any security yet.)


<?php 

namespace App\classes\upload {

use App\User;

    
class upload {

   protected $file;
   protected $path;
   protected $user;
 
    public function __construct(file $file,$path,User $user)) {
    $this->file = $file;
        $this->path = $path;
        $this->user = $user;
    }

    public function getFileName()
    {
        $name = time() .$this->file->getClientOriginalName();
        return $name;
    }

    public function saveFile()
    {
        foreach ($this->$file as $f) {
            $f->move($this->$path);
            $ticket->image()->create([$path.$this->user->id.'/'.$this-          >getFileName(),'name' => $this->getFileName()]);
            $this->file->move($this->path);
        }
    }
}
?>

How can I declare the $file? Right now I've just done file.

This is my controller:

       $file = $request->file('file');
        $upload = new upload($file,'files/'.$userid,Auth::user()->id);
        $upload->saveFile();
0 likes
1 reply
TortleWortle's avatar

In your controller you're passing through the user id but the class expects an user object. and I think in your Construct method you need to do "File $file" or Even "UploadedFile $file" I'm not sure on either of those.

Please or to participate in this conversation.