step 1 - App/config App.php base Url
<?php
namespace Config;
use CodeIgniter\Config\BaseConfig;
class App extends BaseConfig
{
/**
* --------------------------------------------------------------------------
* Base Site URL
* --------------------------------------------------------------------------
*
* URL to your CodeIgniter root. Typically, this will be your base URL,
* WITH a trailing slash:
*
* http://example.com/
*/
public string $baseURL = 'http://127.0.0.1/multimage/public/';
step 2 - Create database and define in Database.php
<?php
namespace Config;
use CodeIgniter\Database\Config;
/**
* Database Configuration
*/
class Database extends Config
{
/**
* The directory that holds the Migrations
* and Seeds directories.
*/
public string $filesPath = APPPATH . 'Database' . DIRECTORY_SEPARATOR;
/**
* Lets you choose which connection group to
* use if no other is specified.
*/
public string $defaultGroup = 'default';
/**
* The default database connection.
*/
public array $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'ajaxcrud',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => true,
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
'numberNative' => false,
];
/**
* This database connection is used when
* running PHPUnit database tests.
*/
public array $tests = [
'DSN' => '',
'hostname' => '127.0.0.1',
'username' => '',
'password' => '',
'database' => ':memory:',
'DBDriver' => 'SQLite3',
'DBPrefix' => 'db_', // Needed to ensure we're working
correctly with prefixes live. DO NOT REMOVE FOR CI DEVS
'pConnect' => false,
'DBDebug' => true,
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
'foreignKeys' => true,
'busyTimeout' => 1000,
];
step 3 - Create Model and define table fields in StudentModel.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class StudentModel extends Model{
protected $table ="students";
protected $primaryKey = "id";
protected $allowedFields = ['image'];
}
step 4 - Create and define routes in Routes.php :
<?php
use CodeIgniter\Router\RouteCollection;
/**
* @var RouteCollection $routes
*/
$routes->get('/imgform','Home::imgform');
$routes->post('/upload','Home::upload');
step 5 - Create view file imgform.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,
shrink-to-fit=no">
<title>Upload Multiple Files or Images in Codeigniter 4 </title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<style>
.container {
max-width: 500px;
}
</style>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js">
</script>
</head>
<body>
<div class="container mt-5">
<?php if (session('msg')) : ?>
<div class="alert alert-success mt-3">
<?= session('msg') ?>
</div>
<?php endif ?>
<div class="col-lg-12">
<h3>Multiple Image Upload By CI4</h3>
</div>
<form method="post" action="<?php echo base_url('/upload');?>"
enctype="multipart/form-data">
<div class="form-group mt-3">
<input type="file" name='images[]' multiple="" id="images"
class="form-control">
</div>
<div class="form-group">
<button type="submit" id="submit" class="btn btn-danger">Upload</button>
</div>
</form>
</div>
</body>
</html>
step 6 - To create Controller Home.php
<?php
namespace App\Controllers;
use App\Models\StudentModel;
class Home extends BaseController
{
// upload images functions
public function imgform(){
return view('imgform');
}
public function upload(){
$model = new StudentModel();
$msg = 'Please select a valid files';
if ($this->request->getFileMultiple('images')) {
foreach($this->request->getFileMultiple('images') as $file)
{
$file->move(WRITEPATH . 'uploads');
$data = [
'image' => $file->getClientName(),
// 'type' => $file->getClientMimeType()
];
$save = $model->save($data);
$msg = 'Files have been successfully uploaded';
}
}
return redirect()->to( base_url('/imgform') )->with('msg', $msg);
}
}


No comments:
Post a Comment