<?php
# import class pada controller
use MiniMVC\System\Storage;
# buat object
$storage = new Storage();
# target directory
$directory = "temp/;
# call functionnya
$data = $storage->Upload("gambar", $directory, false);
# result
var_dump($data);
result berupa object
object(stdClass)[57]
public 'image_original_name' => string 'preview.jpg' (length=11)
public 'image_name' => string '86e8985aa845f5a82b2b72e2b7bed713.jpg' (length=36)
public 'image_type' => string 'image/jpeg' (length=10)
public 'image_size' => int 1238922
public 'image_error' => int 0
update files ()
untuk melakukan update files, files yang di update akan menghapus file lama dan mereplacenya dengan files baru mempunyai empat parameter
<?php
use MiniMVC\System\Storage;
# buat object
$storage = new Storage();
# target directory
$directory = temp_dir();
# call functionnya
# $oldfiles = $directory . "namafilesnya";
#example
$oldfiles = $directory . "deecbcc27b035a55cc5692326d24e09e.png";
$data = $storage->UpdateFile($oldfiles, "gambar", $directory, true);
# result object
var_dump($data);
# get nama files
echo $data->image_name; #or
# save nama files to variabel
$image = $data->image_name;
interacting image with database
untuk interacsi dengan data didatabase
# uploaded algorithm
# uploade image
# 1. uploaded files images
# 2. getting name files image
# 3. insert name files image to database
# update images
# 1. update files image
# 2. getting name files image
# 3. update name files image to database
Files Upload ( pdf , exel, doc )
sedang dalam uji coba
upload csv or excel
untuk upload files berformas csv atau excel dapat menggunakan libraries office yang dibuat yaitu read_file_csv(). untuk mengubah field pada csv menjadi array. setelah berubah menjadi array bisa di coba melakukan looping insert data.
use MiniMvc\Apps\Libraries\office; // libraries
$office = new office; // buat object baru
$office->read_file_csv("namefile"); // return object
example
arah routenya web.php
web.php
$router->get('office/upload', function () {
Routes::Routing("officeController", "index");
});
$router->post('office/upload', function () {
Routes::Routing("officeController", "upload");
});
use MiniMvc\Apps\Libraries\office;
class welcome extends Controller
{
public function __construct()
{
// code here
}
public function index()
{
view("office_view");
}
public function upload()
{
$office = new office;
$data = $office->read_file_csv("document"); // name di input html
dump($data); // debug return object
}
}
Multiple Insert
untuk multiple insert data, hasil membaca file dari file excel itu bisa dilakukan dengan looping
example hasil read file excel *covid-19 data example*,
class DocumentModel
{
private $table = 'documents';
private $db;
public function __construct()
{
$this->db = new Database;
}
public function save($tanggal, $odp, $pdp, $positif, $sembuh, $meninggal)
{
// query data insert
// (`id`, `tanggal`, `odp`, `pdp`, `positif`, `sembuh`, `meninggal`);
$query = "INSERT INTO $this->table VALUES ('',:tanggal,:odp,:pdp,:positif,:sembuh,:meninggal)";
$this->db->query($query);
// binding untuk data string
$this->db->bind('tanggal', $tanggal);
$this->db->bind('odp', $odp);
$this->db->bind('pdp', $pdp);
$this->db->bind('positif', $positif);
$this->db->bind('sembuh', $sembuh);
$this->db->bind('meninggal', $meninggal);
// execute insert atau save data
$this->db->execute();
return $this->db->rowCount();
}
}
pada controllernya
public function store()
{
// code here store here
$file_excel = office::read_file_csv("files");
// remove column
unset($file_excel->worksheet[0]);
// call model
$excel = model("DocumentModel");
// loop mutiple insert
foreach($file_excel->worksheet as $key){
$excel->save($key[0],$key[1],$key[2],$key[3],$key[4],$key[5]);
}
}