CodeIgniter Forums
How to display uploaded file from writable/uploads - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: How to display uploaded file from writable/uploads (/showthread.php?tid=93031)



How to display uploaded file from writable/uploads - paulgough - 06-17-2025

Hello,
I have a question. In CodeIgniter 4, how can I display the path of an uploaded file from the writable/uploads folder?

I upload various file types including images, documents, PDFs, and CSVs using the following code and store the file paths in the database.
Code:
$file->move(WRITEPATH . 'uploads', $fileName);


All uploaded records are listed on a page, and I need to open the uploaded CSV or Excel files in a new tab from this listing page.

However, I’m unable to retrieve the path from the writable/uploads folder to open it in a new tab.

Code:
<?=WRITEPATH.'uploads/'.$allList->documentCsv?>


When I copy the link and try to open it in a new tab, it downloads instead of displaying the file.

Could you please explain how to properly show the file path?


RE: How to display uploaded file from writable/uploads - InsiteFX - 06-17-2025

This may get you going in the right path.

PHP Code:
// In your controller or a suitable method
public function readAndDisplayCsv()
{
    // Replace 'your_csv_file.csv' with the actual filename
    $filePath WRITEPATH 'uploads/your_csv_file.csv';

    if (file_exists($filePath)) {
        $csvData = [];
        if (($handle fopen($filePath"r")) !== FALSE) {
            while (($data fgetcsv($handle1000",")) !== FALSE) {
                $csvData[] = $data;
            }
            fclose($handle);
        }

        // Pass $csvData to your view for display
        return view('csv_display_view', ['csvData' => $csvData]);
    } else {
        // Handle file not found error
        return $this->response->setStatusCode(404)->setBody('File not found');
    }