Cara Upload file di codeigniter 4

Cara Upload file di codeigniter 4

Cara Upload file di codeigniter 4

Kami akan berbagi tutorial Cara Upload file di codeigniter 4 ikuti langkahnya dibawah ini

Buat Controller

<?php namespace App\Controllers;

use CodeIgniter\Controller;

use App\Models\ContactModel;

class Contact extends BaseController

{

public function __construct(){

}

public function index()

{

return view('form');

}

public function upload()

{




helper(['form', 'url']);




$db = \Config\Database::connect();

$builder = $db->table('file');




$validated = $this->validate([

'file' => [

'uploaded[file]',

'mime_in[file,image/jpg,image/jpeg,image/gif,image/png]',

'max_size[file,4096]',

],

]);




$msg = 'Please select a valid file';




if ($validated) {

$avatar = $this->request->getFile('file');

$avatar->move(WRITEPATH . 'uploads');




$data = [




'name' => $avatar->getClientName(),

'type' => $avatar->getClientMimeType()

];




$save = $builder->insert($data);

$msg = 'File has been uploaded';

}




return redirect()->to( base_url('contact/form') )->with('msg', $msg);




}

}

 

Buat View

form.php

 

 

Baca Juga  Cara install codeigniter 4 dengan composer

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Need Help? Chat with us