note 130459 added to function.readfile

From: Date: Tue, 26 Aug 2025 12:09:43 +0000
Subject: note 130459 added to function.readfile
Groups: php.notes 
Request: Send a blank email to php-notes+get-222318@lists.php.net to get a copy of this message
* context: readfile outputs html gibberish to a csv file * environment: Linux with Litespeed tweak to your needs === download function === class Filesystem { public static function download(string $filePathName, string $mime, bool $shouldDelete) { if(file_exists($filePathName)) { header('Content-Description: File Transfer'); header("Content-Type: $mime"); header('Content-Disposition: attachment; filename="' . basename($filePathName) . '"'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($filePathName)); readfile($filePathName); } clearstatcache(); if ($shouldDelete && file_exists($filePathName)) { unlink($filePathName); } throw new \Throwable('FileDownloaded'); } } === wrapper function === class MyController { public function download($fileName) { try { FileSystem::download( storage_path("app/private/database/$fileName"), 'text/csv', true ); } catch (\Throwable $e) { if ($e->getMessage() == 'FileDownloaded') { // redirect back } throw $e; } } } ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 62.21.32.199) ---- Manual Page -- https://php.net/manual/en/function.readfile.php Edit -- https://main.php.net/note/edit/130459 Del: integrated -- https://main.php.net/note/delete/130459/integrated Del: useless -- https://main.php.net/note/delete/130459/useless Del: bad code -- https://main.php.net/note/delete/130459/bad+code Del: spam -- https://main.php.net/note/delete/130459/spam Del: non-english -- https://main.php.net/note/delete/130459/non-english Del: in docs -- https://main.php.net/note/delete/130459/in+docs Del: other reasons-- https://main.php.net/note/delete/130459 Reject -- https://main.php.net/note/reject/130459 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222318) next »