Downloading epub file from server using php

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

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

var_dump($_POST);

mkdir($curBookTempFolder);
chmod($curBookTempFolder, 0777);

$dir=$curBookFolder.'/';
$dirNew=$curBookTempFolder;
$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)) {
            continue;
        }
        if (is_dir($file)) {
            //$zip->addEmptyDir(str_replace("$dir", '', "$file/"));
        } elseif (is_file($file)) {
            $zip->addFromString(
                str_replace("$dir", '', $file),
                file_get_contents($file)
            );
        }
    }
    $zip->close();

    rename($zipFile, $epubFile);
    header("Content-type: application/epub+zip");
    header("Content-disposition:attachment;filename=".basename($epubFile));
    header("Content-Transfer-Encoding: binary");
    readfile($epubFile);

?>

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.
JohnLourduAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Uhh, why did you rename zip to epub?
0
JohnLourduAuthor Commented:
I want to download epub file not zip. Is it possible without using php rename command to download epub file?
0
Ray PaseurCommented:
The epub file isn't the same kind of file as the zip file.  So you might need different processes to create the epub.
http://en.wikipedia.org/wiki/EPUB
http://en.wikipedia.org/wiki/ZIP_%28file_format%29
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

JohnLourduAuthor Commented:
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?
0
Ray PaseurCommented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
You can look here for more information: http://en.wikipedia.org/wiki/EPUB
0
JohnLourduAuthor Commented:
Thanks for your suggestions. I have solved this issue to avoid using php's content disposition. I have used below code snippet. It works fine for me.
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

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

var_dump($_POST);

mkdir($curBookTempFolder);
chmod($curBookTempFolder, 0777);

$dir=$curBookFolder.'/';
$dirNew=$curBookTempFolder;
$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)) {
            continue;
        }
        if (is_dir($file)) {
            //$zip->addEmptyDir(str_replace("$dir", '', "$file/"));
        } elseif (is_file($file)) {
            $zip->addFromString(
                str_replace("$dir", '', $file),
                file_get_contents($file)
            );
        }
    }
    $zip->close();

    rename($zipFile, $epubFile);
    header("location:epub_content/sampBook/book.epub");
    unlink($curBookTempFolder);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnLourduAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.