Download file header issue

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
kennm
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Thu Jul 26, 2018 12:54 pm

Download file header issue

Post by kennm » Fri Mar 01, 2024 2:33 pm

I created a component wich purpose is to download the file by a user. It's a bit complex to explain it briefly, but to put things simple, I want the file to be downloaded with use of PHP code (therea are many ifs - what and whether is downloaded, depanding on the context - which don't matter to the issue). This is the single header in the PHP code that causes the user being forced to sign-in to the website:

Code: Select all

header('Content-Disposition: attachment');
However I don't want Joomla to force the user to sign in. When I remove this header, Joomla "magically" stops requiring the user to sign-in - which is what I want, i.e. the user should NOT be redirected to the login page. But then, instead the file is openning in the browser - which is not want I want. How can I make user download the file, not open in the browser, and not require to sign in (as then Joomla, as mentioned, redirects the user to the login page)?

User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2724
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Download file header issue

Post by ceford » Sat Mar 02, 2024 5:57 am

From https://docs.joomla.org/J4.x:Joomla_4_T ... xport_Data:

Code: Select all

		$app->setHeader('Content-Type', 'text/csv; charset=utf-8', true);
		$app->setHeader('Content-disposition', 'attachment; filename="' . $filename . '-' . $date . '.csv"', true);
		$app->setHeader('Cache-Control', 'no-cache', true);
		$app->sendHeaders();
Hope this helps.


Post Reply

Return to “Joomla! 4.x Coding”