Avatar of JohnLourdu
JohnLourduFlag for Afghanistan asked on

Downloading epub file from server using php

I have created php script as mentioned below for dynamic downloading of epub files:
ini_set('display_errors', TRUE);

$curBookFolder = $_POST['curBookFolder'];
$curBookFolderOPS = $_POST['curBookFolderOPS'];
$curBookTempFolder = $_POST['curBookTempFolder'];


chmod($curBookTempFolder, 0777);

$zipFile = $curBookTempFolder . 'book.zip';
$epubFile = $curBookTempFolder . 'book.epub';
$mimetype = 'epub_content/model/mimetype';

$excludes = array('.DS_Store', 'iTunesMetadata.plist');

system('zip -q0Xj $epubFile $mimetype');

$zip = new ZipArchive();
    if ($zip->open($zipFile, ZipArchive::CREATE) != true) {
        throw new Exception("Unable open archive '$zipFile'");

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);

foreach ($files as $file) {
        if (in_array(basename($file), $excludes)) {
        if (is_dir($file)) {
            //$zip->addEmptyDir(str_replace("$dir", '', "$file/"));
        } elseif (is_file($file)) {
                str_replace("$dir", '', $file),

    rename($zipFile, $epubFile);
    header("Content-type: application/epub+zip");
    header("Content-Transfer-Encoding: binary");


Open in new window

Before rename, the zip file downloaded and extracted fine. But I have renamed zip to epub and download using above script, EPUB file not opened. I have received 'The Compressed(zipped) Folder ... is invalid. Please advice how to solve this.
Web Languages and StandardsPHPWeb Components

Avatar of undefined
Last Comment

8/22/2022 - Mon
Ray Paseur

Uhh, why did you rename zip to epub?

I want to download epub file not zip. Is it possible without using php rename command to download epub file?
Ray Paseur

The epub file isn't the same kind of file as the zip file.  So you might need different processes to create the epub.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

OK. I understand. As per epub standard requires the mimetype file be added at the start of the archive as uncompressed. But PHP's built in Zip Archive class doesn't support adding files to an archive that are uncompressed. So I have used below mentioned PHP code:
system('zip -q0Xj $filename $mimetype');

Open in new window

I think above code not responded in Zip Archive. So how to solve this issue. Please help?
Ray Paseur

I really don't know what to do.  It doesn't seem to be a question with any obvious answer.  It might be "you can't get there from here" or it might be a research project.  Suggest you leave the question open a little longer -- maybe one of the other experts can help.
Dave Baldwin

You can look here for more information: http://en.wikipedia.org/wiki/EPUB
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

The main reason for corrupting file due to using php's content-type, content-disposition and content-transfer-encoding. These properties change the file and invalid. So I remove these properties and directly open file using absolute path.