JohnLourdu
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);
?>
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.
Uhh, why did you rename zip to epub?
ASKER
I want to download epub file not zip. Is it possible without using php rename command to download epub file?
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
http://en.wikipedia.org/wiki/EPUB
http://en.wikipedia.org/wiki/ZIP_%28file_format%29
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');
I think above code not responded in Zip Archive. So how to solve this issue. Please help?
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.
You can look here for more information: http://en.wikipedia.org/wiki/EPUB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.