troubleshooting Question

Downloading epub file from server using php

Avatar of JohnLourdu
JohnLourduFlag for Afghanistan asked on
Web Languages and StandardsPHPWeb Components
8 Comments1 Solution1487 ViewsLast Modified:
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);

?>
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.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros