Skip to content

Commit 735996d

Browse files
committed
✨ added download functionality
1 parent 8a6dc75 commit 735996d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Response.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,32 @@ public static function json($data, int $code = 200, bool $showCode = false, bool
129129
echo json_encode($dataToPrint);
130130
}
131131

132+
133+
/**
134+
* Output plain text
135+
*
136+
* @param string $file Path to the file to download
137+
* @param string|null $name The of the file as shown to user
138+
* @param int $code The response status code
139+
*/
140+
public static function download(string $file, string $name = null, int $code = 200)
141+
{
142+
if (!file_exists($file)) {
143+
trigger_error("$file not found. Confirm your file path.");
144+
}
145+
146+
if ($name === null) $name = basename($file);
147+
148+
Headers::status($code);
149+
Headers::set([
150+
'Content-Length' => filesize($file),
151+
'Content-Disposition' => "attachment; filename=$name",
152+
]);
153+
154+
readfile($file);
155+
exit;
156+
}
157+
132158
/**
133159
* Throw an error and break the application
134160
*/

0 commit comments

Comments
 (0)