Demonstrates using view() to serve files to the browser with proper HTTP headers.
- Serving a file inline (displayed in the browser) with
view(false) - Forcing a download dialog with
view(true) - Automatic HTTP header management:
Content-Typebased on MIME detectionAccept-Ranges: bytesfor range request supportContent-LengthContent-Dispositionwith the filename (inline or attachment)Content-Rangefor partial content responses (HTTP 206)
This example must be run with a web server, not CLI:
php -S localhost:8080 examples/serving-files.phpThen visit:
http://localhost:8080— displays the file inlinehttp://localhost:8080?download=1— triggers a download dialog
File::view(bool $asAttachment = false)— Sends the file content to the client with appropriate HTTP headers. If raw data is empty, it callsread()first. Passtrueto force a download instead of inline display.