CodeIgniter Forums
Working with uploaded files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Working with uploaded files (/showthread.php?tid=82170)



Working with uploaded files - Kordianz - 06-20-2022

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


RE: Working with uploaded files - Kordianz - 06-24-2022

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