Welcome Guest, Not a member yet? Register   Sign In
How to display uploaded file from writable/uploads
#1

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?
Reply
#2

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');
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB