79378274

Date: 2025-01-22 15:14:57
Score: 0.5
Natty:
Report link
// 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));
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: maulik