Joomla 3 PHP script added to Joomla 4 broken

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
Darrell_LeRoy
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sun Aug 23, 2020 6:36 pm

Joomla 3 PHP script added to Joomla 4 broken

Post by Darrell_LeRoy » Fri Sep 15, 2023 10:07 pm

I have a PHP script added to Joomla that worked in Joomla 3. Joomla 4 broke the script.
The script is
<html>
<body>

<?php
$filename = $_POST['filename'];
$aa=$filename;
if ($aa == null) {
$b = "error";
} else {
$b = $aa ;
}
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=$b');
header('Content-Transfer-Encoding: binary');
readfile($b);

?>
</body>
</html>
In J4, it displays the PDF file in raw form instead of showing the page.
Any help?
Last edited by toivo on Fri Sep 15, 2023 10:23 pm, edited 1 time in total.
Reason: mod note: retitled

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2774
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Joomla 3 PHP script added to Joomla 4 broken

Post by SharkyKZ » Sat Sep 16, 2023 12:29 pm

Added where?

Darrell_LeRoy
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sun Aug 23, 2020 6:36 pm

Re: Joomla 3 PHP script added to Joomla 4 broken

Post by Darrell_LeRoy » Sun Sep 17, 2023 3:05 am

I have a form page that asks for a name. That page goes to a PHP script in a folder that searches that folder for that name and returns it with the option to view it. When you click view, that calls the referred script.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2774
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Joomla 3 PHP script added to Joomla 4 broken

Post by SharkyKZ » Sun Sep 17, 2023 10:14 am

It's still unclear to me if this is an external script or not. In case it does runs through Joomla, the issue could be that you're using header() function instead of application's setHeader() method https://api.joomla.org/framework-2/clas ... _setHeader. Headers sent by application, overwrite those set beforehand. You can check this by inspecting the response headers in your browser.

User avatar
eorisis
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Sun Jul 21, 2013 5:18 pm
Location: Thessaloniki Greece
Contact:

Re: Joomla 3 PHP script added to Joomla 4 broken

Post by eorisis » Fri Sep 22, 2023 8:54 am

Darrell_LeRoy wrote:
Fri Sep 15, 2023 10:07 pm
I have a PHP script added to Joomla that worked in Joomla 3. Joomla 4 broke the script.
The script is
<html>
<body>

<?php
$filename = $_POST['filename'];
$aa=$filename;
if ($aa == null) {
$b = "error";
} else {
$b = $aa ;
}
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=$b');
header('Content-Transfer-Encoding: binary');
readfile($b);

?>
</body>
</html>
In J4, it displays the PDF file in raw form instead of showing the page.
Any help?
If you use this in a separate file that you call from the url, you must exit right after readfile(). This means you got to remove all the html, body tags and not use a closing php tag at the end either. Start your file with <?php and nothing before that. Overall the code is not good.
Joomla-Powered IT Solutions
https://eorisis.com


Post Reply

Return to “Joomla! 4.x Coding”