PHP Dynamically Generated Zip File by ZipArchive Can't be downloaded
I am trying to create a zip file and download that zip file on a button click in WordPress but when I try to download the file it always throws error -
Cannot modify header information - headers already sent by
Here is my code -
function downloadZipFile($file_array){
# create new zip object
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($file_array as $file) {
//echo $file;
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
echo ABSPATH.'downloadZip.php';
header('Content-disposition: attachment; filename="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
}
In the above function I am passing a file array which is coming from box api. I can see all files are coming fine from box api endpoint.
php wordpress
add a comment |
I am trying to create a zip file and download that zip file on a button click in WordPress but when I try to download the file it always throws error -
Cannot modify header information - headers already sent by
Here is my code -
function downloadZipFile($file_array){
# create new zip object
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($file_array as $file) {
//echo $file;
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
echo ABSPATH.'downloadZip.php';
header('Content-disposition: attachment; filename="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
}
In the above function I am passing a file array which is coming from box api. I can see all files are coming fine from box api endpoint.
php wordpress
did you check if the file you are trying to zip have the permission ?
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:29
2
Possible duplicate of How to fix "Headers already sent" error in PHP
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:47
@MohammedYassineCHABLI Yes files are having permission.
– vivek kumar
Dec 31 '18 at 9:20
I don't think this is duplicate of stackoverflow.com/questions/8028957/…. I fixed this error by using ob_start() function in wp-config.php but my zip file is always empty. I think think this is a problem with box api because when I try with other API URL it works. I also found this link community.box.com/t5/Platform-and-Development-Forum/….
– vivek kumar
Dec 31 '18 at 9:30
add a comment |
I am trying to create a zip file and download that zip file on a button click in WordPress but when I try to download the file it always throws error -
Cannot modify header information - headers already sent by
Here is my code -
function downloadZipFile($file_array){
# create new zip object
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($file_array as $file) {
//echo $file;
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
echo ABSPATH.'downloadZip.php';
header('Content-disposition: attachment; filename="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
}
In the above function I am passing a file array which is coming from box api. I can see all files are coming fine from box api endpoint.
php wordpress
I am trying to create a zip file and download that zip file on a button click in WordPress but when I try to download the file it always throws error -
Cannot modify header information - headers already sent by
Here is my code -
function downloadZipFile($file_array){
# create new zip object
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($file_array as $file) {
//echo $file;
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
echo ABSPATH.'downloadZip.php';
header('Content-disposition: attachment; filename="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
}
In the above function I am passing a file array which is coming from box api. I can see all files are coming fine from box api endpoint.
php wordpress
php wordpress
asked Dec 31 '18 at 8:14
vivek kumarvivek kumar
209
209
did you check if the file you are trying to zip have the permission ?
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:29
2
Possible duplicate of How to fix "Headers already sent" error in PHP
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:47
@MohammedYassineCHABLI Yes files are having permission.
– vivek kumar
Dec 31 '18 at 9:20
I don't think this is duplicate of stackoverflow.com/questions/8028957/…. I fixed this error by using ob_start() function in wp-config.php but my zip file is always empty. I think think this is a problem with box api because when I try with other API URL it works. I also found this link community.box.com/t5/Platform-and-Development-Forum/….
– vivek kumar
Dec 31 '18 at 9:30
add a comment |
did you check if the file you are trying to zip have the permission ?
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:29
2
Possible duplicate of How to fix "Headers already sent" error in PHP
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:47
@MohammedYassineCHABLI Yes files are having permission.
– vivek kumar
Dec 31 '18 at 9:20
I don't think this is duplicate of stackoverflow.com/questions/8028957/…. I fixed this error by using ob_start() function in wp-config.php but my zip file is always empty. I think think this is a problem with box api because when I try with other API URL it works. I also found this link community.box.com/t5/Platform-and-Development-Forum/….
– vivek kumar
Dec 31 '18 at 9:30
did you check if the file you are trying to zip have the permission ?
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:29
did you check if the file you are trying to zip have the permission ?
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:29
2
2
Possible duplicate of How to fix "Headers already sent" error in PHP
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:47
Possible duplicate of How to fix "Headers already sent" error in PHP
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:47
@MohammedYassineCHABLI Yes files are having permission.
– vivek kumar
Dec 31 '18 at 9:20
@MohammedYassineCHABLI Yes files are having permission.
– vivek kumar
Dec 31 '18 at 9:20
I don't think this is duplicate of stackoverflow.com/questions/8028957/…. I fixed this error by using ob_start() function in wp-config.php but my zip file is always empty. I think think this is a problem with box api because when I try with other API URL it works. I also found this link community.box.com/t5/Platform-and-Development-Forum/….
– vivek kumar
Dec 31 '18 at 9:30
I don't think this is duplicate of stackoverflow.com/questions/8028957/…. I fixed this error by using ob_start() function in wp-config.php but my zip file is always empty. I think think this is a problem with box api because when I try with other API URL it works. I also found this link community.box.com/t5/Platform-and-Development-Forum/….
– vivek kumar
Dec 31 '18 at 9:30
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985123%2fphp-dynamically-generated-zip-file-by-ziparchive-cant-be-downloaded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985123%2fphp-dynamically-generated-zip-file-by-ziparchive-cant-be-downloaded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
did you check if the file you are trying to zip have the permission ?
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:29
2
Possible duplicate of How to fix "Headers already sent" error in PHP
– Mohammed Yassine CHABLI
Dec 31 '18 at 8:47
@MohammedYassineCHABLI Yes files are having permission.
– vivek kumar
Dec 31 '18 at 9:20
I don't think this is duplicate of stackoverflow.com/questions/8028957/…. I fixed this error by using ob_start() function in wp-config.php but my zip file is always empty. I think think this is a problem with box api because when I try with other API URL it works. I also found this link community.box.com/t5/Platform-and-Development-Forum/….
– vivek kumar
Dec 31 '18 at 9:30