[Image]
Menyimpan sebuah gambar ke dalam sebuah web merupakan suatu kebutuhan dalam menambah konten website yang dinamik. Berikut tutorial kecil untuk menyimpan sebuah gambar ataupun file lainnya ke dalam aplikasi.
Kode untuk form add:
Pastikan di dalam form ditambahkan option untuk POST file:
$this->form->create($yourvariable,['type'=>'file']);
atau
$this->Form->create($document, ['enctype' => 'multipart/form-data']);
Kode untuk function add:
if($this->request->data['image']){
// get properties of image object
$img = $_FILES['image'];
$name = $img['name'];
$type = $img['type'];
$tmp_name = $img['tmp_name'];
$error = $img['error'];
$size = $img['size'];
// setting directory upload
$target_dir = "uploads/vintage/"; // sample folder
// setting webroot
$target_name = WWW_ROOT.$target_dir.$name;
// cek folder
if(!is_dir(WWW_ROOT.$target_dir)){
mkdir(WWW_ROOT.$target_dir);
}
if(is_uploaded_file($tmp_name)){
move_uploaded_file($tmp_name, $target_name);
}
$collection->image_name = $name;
$collection->image_file_type = $type;
$collection->url = $target_dir.$name;
}
Keterangan kode: $this->request->data['image'] : mengambil data dari form dengan input "image". WWW_ROOT : full path untuk menuju webroot. sumber is_uploaded_file() : fungsi bawaan php dengan nilai return true/false untuk memastikan file kita telah terupload via HTTP POST. sumber
posted by Brillian Musyafa at 2:58 PM on Aug 21, 2015
"Tutorial Cakephp 3 Save File"
No comments yet. -