While fixing a download issue related to CSV, I have added some headers that are commonly available in PHP. After, adding those I have performed a CSV export test. From that point onward in my browser, a new tab is opening & closing infinitely. I was not able to stop it by reverting the update that I have made. Cleared server & browser - cache & history. Still, the tabs are reopening while trying to export with older code. I'm now stopping the browser(Chrome) by executingsudo pkill -9 chrome
on the terminal.
I think the new tab opening & closing is related to the force download code that I have added.
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Type: application/csv;');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
// file creation
$file = fopen('php://output', 'w');
$header = array("Group Name","Contact Name","Email","Phone Number","Company Name");
fputcsv($file, $header);
foreach ($new_array as $key => $line){
fputcsv($file,$line);
}
fclose($file);
exit;
$new_array
is an array of data taken from DB.
I think the issue was because of the reason that I forgotten to add cache header like:
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
Is there any way to stop this issue?
A video link to see the issue
@MagnusEriksson's comments were correct.
It was not an issue with PHP/JS or even with the browser.
I was asked to test the CSV file by open in the browser to check the issue & I have tried the "open with" option provided by the system. And that resulted in the issue, the downloaded file after clicking the export option was trying to open in the browser window which led to downloaded the same file again & it went on like an infinite loop.
This is not a code-related issue but it has taken my whole day. I have searched a lot related to PHP headers.
I'm also adding a few valuable comments from @MagnusEriksson's below:
No PHP code can make tabs in the browser open and close.
You seem to have two different problems. 1. There's no download (which most likely is PHP). 2. The tab keeps opening and closing (this can not be caused by PHP since PHP can't open or close tabs in the client). That looks like some JS/browser issue.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.