Welcome Guest, Not a member yet? Register   Sign In
Working with uploaded files
#1

Hi,
I work on CRM system for my friend, and I'm stoped by this theoretically think.
For test, I create upload form (frome some tutorial):
Code:
public function upload()
    {
        $validationRule = [
            'userfile' => [
                'label' => 'Image File',
                'rules' => 'uploaded[userfile]'
                   
                    . '|mime_in[userfile,image/jpg,image/jpeg,image/gif,image/png,image/webp,application/pdf]'
                    ,
            ],
        ];
        if (! $this->validate($validationRule)) {
            $data = ['errors' => $this->validator->getErrors()];

            return view('ADMIN/upload', $data);
        }

        $img = $this->request->getFile('userfile');
        print_r($img);
        if (! $img->hasMoved()) {
            $filepath =  $img->store();
            echo $filepath.'<br>';
            $filepath2=WRITEPATH . 'uploads/' .$filepath;
            echo $filepath2;

            $data = ['uploaded_flleinfo' => new File($filepath2)];
           
           


            return view('ADMIN/upload_success', $data);
        }
        $data = ['errors' => 'The file has already been moved.'];

        return view('ADMIN/upload', $data);
    }
In view upload_succes I have acces to part of file parameters:
Code:
<ul>
    <li>name: <?= esc($uploaded_flleinfo->getBasename()) ?></li>
    <li>name: <?= esc($uploaded_flleinfo->getRealPath()) ?></li>
    <li>size: <?= esc($uploaded_flleinfo->getSizeByUnit('kb')) ?> KB</li>
    <li>extension: <?= esc($uploaded_flleinfo->guessExtension()) ?></li>
    <li>extension: <?= $uploaded_flleinfo->getMimeType() ?></li>
</ul>
Files must be in writable, not public. 
they must works for logged in users only, and i try how to grant acces to this files. For download or show (when it is picture).
Can someone helps?
thanks
Reply
#2

OK, after search I find reponse class and service
$response = service('response');
return $response->download($file,null);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB