Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to properly download file from the server with FlightPHP #600

Closed
pierresh opened this issue Jul 21, 2024 · 5 comments
Closed

Comments

@pierresh
Copy link

Hello,

I am sorry for this trivial question, but I wonder what is the cleanest way to download a file from an end-point with FightPHP? I could not see anything related in the documentation.
Currently, I am doing as shown below, but in the same way that Symfony has BinaryFileResponse, maybe it would be better to have something similar?

So far

// 1. Retrieve my file $attachment
// 2. Deal with headers

ob_end_clean();
flush();

if (!readfile($attachment)) {
	Flight::error(new Exception('Cannot read file ' . $attachment));
}
@n0nag0n
Copy link
Collaborator

n0nag0n commented Jul 21, 2024

Does this example help you out in the streaming portion of the docs? https://docs.flightphp.com/learn/routing#streaming

@pierresh
Copy link
Author

Thanks for your quick feedback. I did not know it was possible to stream files :).

Unfortunately, I am using wildcards in my GET router because I have hundreds of routes, so I could not use the callback. I will try to submit a PR for a helper function to download files later this week.

@n0nag0n
Copy link
Collaborator

n0nag0n commented Jul 22, 2024

Could you give me a pseudo code example of your codebase and how you're trying to do it? Maybe there's another way?

@pierresh
Copy link
Author

pierresh commented Jul 26, 2024

Actually, what I wish for is a simple way to handle files, as easy as using Flight::json() for sending a JSON response.

I use a customized Kernel to load Controllers based on the route, so that I do not need to bother with registering all the routes (my app has several hundred). Basically, it looks like this:

Flight::route('GET|POST|PUT|DELETE|PATCH /@module/@name(/@id(/@sub_name(/@line)))', function($module, $name, $id, $sub_name, $line){
	$request = Flight::request();

	$kernel = new App\Kernel();

	$kernel->handle($request);
});

I created a PR for this if you think this idea is interesting.

@pierresh
Copy link
Author

As the PR is now merged, I close this issue, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants