// Controllers name
namespace App\Controllers;
use CodeIgniter\Controller;
class ImageController extends Controller
{
public function viewImage($imageName)
{
// Start session
$session = session();
// Check if the user is logged in
if (!$session->has('userid')) {
// Redirect to the login page or show a 403 error
return redirect()->to('/');
}
// Define the path to the image
$filePath = WRITEPATH . '../profile_img/' . $imageName;
// Check if the file exists
if (!is_file($filePath)) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
// Serve the file as a response
$fileInfo = new \CodeIgniter\Files\File($filePath);
return $this->response->setHeader('Content-Type', $fileInfo->getMimeType())
->setBody(file_get_contents($filePath));
}
}