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:
<?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.
Web Languages and StandardsPHPWeb Components

Avatar of undefined
Last Comment
JohnLourdu
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Uhh, why did you rename zip to epub?
Avatar of JohnLourdu
JohnLourdu
Flag of Afghanistan image

ASKER

I want to download epub file not zip. Is it possible without using php rename command to download epub file?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
Avatar of JohnLourdu
JohnLourdu
Flag of Afghanistan image

ASKER

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?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

You can look here for more information: http://en.wikipedia.org/wiki/EPUB
ASKER CERTIFIED SOLUTION
Avatar of JohnLourdu
JohnLourdu
Flag of Afghanistan image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of JohnLourdu
JohnLourdu
Flag of Afghanistan image

ASKER

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.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo