PHP脚本不会下载我的epub

时间:2015-04-15 10:09:38

标签: php epub epub3

我依次运行2个脚本,第一个创建电子书并将其存储在服务器上,第二个脚本下载并删除用于创建电子书的所有文件。脚本运行完美,除了下载部分,它没有下载书:(

以下是下载脚本:

$file = "../FYP/eBook.epub";
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    //header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename='.basename($file));
    readfile($file);
    echo "file does exist";
unlink($file);
}

正如你所看到的,香港专业教育学院评论了标题,因为它不起作用,我甚至尝试了应用程序/八位字节流' application / epub + zip'但没有成功。

以下是控制台日志中输出的内容:

PK�X�F���Ҭ�OEBPS/title.xhtmlM���0�w��Zb(J����u���D�E���? �����le%xX���F+�D�<W��z����ih�2hN�r�ob��ij  ��@�mQt�G���    R"k^���{�Y��l��&��j���F�:�x������(Fȩ�x�ș.u������q E��N; +u}�|PK�X�F"���*OEBPS/characters.xhtmlm�1�0E���[�
u�,=I.A
Ŗ��7��:t��~���^��&n��k�eY��Q��cYU�;��
@{��r�A���ɶB�5��y
���fj��*�2+HŲbO$
|��oS*�����v�K��,u��G1ͳ��
���6�Q�Ew׸��;��1�i��E;{�ǂS�m��PK�X�Fv�(��4OEBPS/scene155.xhtmlm���� D��
߸�j����'����J���*�����y4#��g��S��7�m����_�/�0��n��ȳqMT)A"��K�����<*�]Ж�`�8�"G���E�_���ӟgx6ໂ����U�-'��T5e�є��ff�S���Yw��d�肉��S9�l�۶ެ5�Lp�;�>�(ṥ��'�/PK�X�F�"�=�YOEBPS/scene156.xhtml�����0D�|�o�am�D��\�jH��
��&TU{�m4��Y;Y����+'��9�6��>���u�5�L�٩�"e&s��O���r���.8�� �*����(p�n�N{��l�W��t�.�o���4��lS���I��D(�d��4���X���Dt
v{٣��P~�k)���V�K���4��'�����,�2�PK�X�F�����OEBPS/scene161.xhtmlM��
�0��>En�v=�Ծ�>�?Y+[��@���U��@>�f����>�Et��Ec,cU��H���i���q�ꤞme��K���=7��+�"�E����m��W��Fg��[�������Ԕ��}+����h�M��6[:�����PK�X�FYl�,�|OEBPS/scene163.xhtml�PAn� ��+|��e������$qCT�"�}M�U�[O���h��-~͝pD��T)E��iRM۶j�a.ڡ+`H3y4�V'8I?��@{�Nn���$��ȴ{�����y�ԑ����n�k��@p�Fï�ypE�\�:��m�����0}�0:G�>���$Lzx�u��3PX^����g�����x�R���d'<���Sl��ꪼhu�ĵ���PK�X�FF��5��OEBPS/scene310.xhtmlM�A
�0E�=���s�]Y�@���85Ҩ��޾�R�j���C^��fi�W�wĔR�nղxm���{ �]_ ���ք'�a���5ʛ�1�rh��X<��+������X����~��k�t<3�����N�Ի��}8��(و�Bx�-o�/PKtu�Foa�,mimetypeK,(��LN,����O-(MҮ�,PKtu�F�o��META-INF/container.xmlU��� D��
�մ��@ML<k��t�D` P�/֤�{�ݙ7��/��S�4�u[��e(���Ū8dͧ$A�Y��e1�"����19���}���DTF�0��Ս��sm�r��t<�/�k����ș��B[�5��5Pj-Ax����n���\�p%�X���PK^�FG(��1�OEBPS/stylesheet.css}�Mn� ���r��t�M�β�����؞��6�ջ��8JkV���<�n`/GM�o�6��j��׆=(�ؐ?rh���ޓ6 
Aa�{�WXu�Zt5�LS%9<^�~�us����օ�m���������{�긃���szT�H���r8d�E�#ln��,������{X`���v���za��!鳈n�ù8�t�;T�M=S�&�^Xc4���nt1�t�&<Q4�G[�Hu�֡�
�z����K���Z���'���O��   �O���S��̗1�+(Fm��BX�F
�)����PK�X�F���OEBPS/content.opf��͎�0����V.>T��4�*V=���x�!�l��&y�^��Vᔙ��o>�����g3��3��*���d��"��_�IZK�!7�Hm}Ʒ��Z��n]�k6b>��W|�N����jȩۦz��P�E/�8�y�5[����)4w��h>v;�n��
Pj�2'@��
��WV��#�KyH���E����I#1���^0VfF������5z��?�!qTsKi7-�+�y���:M�2����+G�p��E��vd*�r&h<1ΗVҚ<�aW�A�BwV�9�6Pd���Y�62�CMEd]�FI���}������pqI���Y����Iy�ׅ���|"w��$���t�s_��?<J�[������G�y�[�H�t9G����e����&#���ٛ��]��˯,�&q~A\=M\�W����ԫ��������D1| d��X`�=dS:�i�Y��T�+2
�9e%�dC"77DÝP�"xz�D�����Žp����E�7������PK�X�F��;��
OEBPS/toc.ncx���n�0��y
�ʫ`.�f"C5�tQ��Dj���c�   �CC��5���)R����?�w.2����ȼ�R�c�01���"R��<��`@�����R�B˧��S�������V���6Mwhr7����!@6!d���E��H�
yu~�=R��0�G0�]똚O~1�������P24<4���b��������T�T���դ�[�vF.&�k�� �褲�.B
,^������3�٥HXUߟ��Z��
(9fBE�W��dA�C�Q^�b����kT痩b/Xv�]��   �EV/tњ�\�\���?����Sk��S�=k�w���|�CB�ȹ�y�r�A7̨BH�SQȑ�?�v�>@v"�_�u�wF�a��,��ux�Rɶ��*��r��tЍZt��t����k���ۗ��
�gu�]�Z�^�9�������ͷΑ�\۩q�n��s�Xf�N��E�G=xPK]�yF�?�!��7OEBPS/Images/51.png��_hE�'Imch�% �R7>����^�6�M���6gܘ�6����N���;��$w|�
��R|�I��/>��/��b�*DB���/�
},Tg.�("���fY��o�~�����ٝ=;7�������쌖��_8�c/-950`Pq�Lϓ<^$%�G i�����rȰ*o�@���h�ɼ��bb'l0�p��;&�ʞa�h�vU�ޥ+�-U8�#��F��̺����s�h&,a�e8YVh",;+n��U�V�B�Y�(�� )��v���9��>���hȌH����˲��F�����bJ\V�cp�hk������N[TS�eB<EK�R�4���(%   VG4�����r��z

L����]�tc�U�Cp��j�`�M�ab��D)�a���6u�]����⣭�B�Cbx�7i�G�)i��W�^�[��؃�����i���x��u%��pM��T���m�R�br&=��'%i:����L,�L��屴���6W��:k����,�i��k���؁�A+v��H����Z��mȊ�3��B]/���nw�*���\��>M��^����DVK�[w�������#�w������ە�\>@gN����f3���Q&zN��W�g�>�������!��z?;H�'N|�CE�{{��0��̷�y>������������MVv�����{m^dvW��[>�9�ˮ�d��
ϲ6����0_�̮|��ط8����������O�:��׾�'q�V��gQ����?
��0����
a䳑���%��?����Ǟ�w�����0թKk+�lDΥ�����?�OR�����m���zw�������??����ʓ����!q�C��8�!q�C��8�!�:t�v?�+Vϩ� �AͲ��7�+��̀���&sv5������p��3��p�+\i��|����o�(�����վL�~�/PK�X�F���Ҭ�OEBPS/title.xhtmlPK�X�F"���*�OEBPS/characters.xhtmlPK�X�Fv�(��4�OEBPS/scene155.xhtmlPK�X�F�"�=�Y�OEBPS/scene156.xhtmlPK�X�F������OEBPS/scene161.xhtmlPK�X�FYl�,�|zOEBPS/scene163.xhtmlPK�X�FF��5���OEBPS/scene310.xhtmlPKtu�Foa�,VmimetypePKtu�F�o���META-INF/container.xmlPK^�FG(��1�iOEBPS/stylesheet.cssPK�X�F����OEBPS/content.opfPK�X�F��;��
�
OEBPS/toc.ncxPK]�yF�?�!��7�OEBPS/Images/51.pngPK

D�file does exist 

这是html / jquery代码:

        $(document).ready(function(){
            retrievePlays();      
        })

        function retrievePlays(){
            $.post("getPlays.php",{
                'uID' : <?php echo $_SESSION['userID']; ?>,
            },function(result){
                for (i =0;i<result.length;i++) {
                 $('<tr/>').attr({'id':'play'+result[i].playid}).appendTo("#myPlays");
                 $('<td/>').html(i+1).appendTo("#play" +result[i].playid);
                 $('<td/>').html(result[i].name).appendTo("#play" +result[i].playid);
                 var app= '<td><button class="edit btn btn-warning" onclick=editPlay(this);>Edit</button></td><td></form><button class="edit btn btn-primary" onclick=downloadPlay(this);>Download</button><td><button class="erase btn btn-danger" onclick=deletePlay(this);>Delete</button></td>';
                 $('#play' +result[i].playid).append(app);
                }
                },"json");
        }

      function editPlay(sender){
        var tr = sender.parentNode.parentNode;
        var idstring = tr.getAttribute('id');
        var id = idstring.substr(4,idstring.length);
        console.log(id);
        location.href = "playwrite.php?playID=" + id;
        }

        function deletePlay(sender) {
        if (confirm('Are you sure you want to remove this play?')) {
        var tr = sender.parentNode.parentNode;
        var idstring = tr.getAttribute('id');
        var id = idstring.substr(4,idstring.length);
        console.log(id);

        $.post("deletePlay.php",{
            'pID' : id,
        },function(){
            $('#play'+id).remove();
        });
        } 
        }

        function downloadPlay(sender) {
            var tr = sender.parentNode.parentNode;
        var idstring = tr.getAttribute('id');
        var id = idstring.substr(4,idstring.length);

      $.post("filetest.php",{
            'pID' : id,},
            function(response){
                console.log(response);
                $.post("deleteFiles.php",{'pID' : id,},function(){});
                });
        }
    </script>
</head>
<body>
    <nav class="navbar navbar-default navbar-fixed-top">
    <ul class="nav navbar-nav">
      <li><a href="main.php">Homepage</a></li>
      <li><a href="settings.html">Settings</a></li>
      <li><a href="playwrite.php">New Play</a></li>
      <li class="Logout"><a href="Login.html">Log Out(<?php echo $_SESSION['username']; ?>)</a></li>
    </ul>
    </nav>
    <div class="mainbox">
        <input id="search_input" placeholder="Type to filter">
        <table id="myPlays" border="1" class="table table-hover table-bordered">
            <th>Number</th>
            <th id="pnames">Play Name</th>
            <th>Edit</th>
            <th>Download</th>
            <th>Delete</th>
        </table>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我正在使用此代码段在我的服务器中下载pdf或doc文件。

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;

答案 1 :(得分:0)

这有很多原因,其中一些可能很难找到。

首先,您的header功能可能会失败。

可能的解决方案1:

这可能是因为在结束标记之后的新线。

而不是:

<?php include 'my_file.php'; ?>

尝试:

<?php include 'my_file.php';

可能的解决方案2:

您的文件可能以UTF8保存。

这不会在屏幕上显示,但它会发送3个附加字节。

尝试保存ASCII,UTF7,UTF8(不含B.O.M),ANSI,windows-1252,ISO-8859-1或ISO-8859-15。

可能的解决方案3:

你正在做echo 'File does exist';

并且您没有指定正确的mime类型。

使用mime-type application/epub+zipsource: wikipedia

删除该行。

可能的解决方案4:

您可能正在输出其他内容。

尝试使用:

ob_start(); //stops the output from being sent
[write your code]

ob_end_clean();//discards the output

include 'your_script.php';

这将捕获并丢弃文件中包含发送新文件的文件的任何输出。


现在,让我们转到新添加的Javascript。

您正在使用jQuery向文件发送POST,然后您对该文件执行某些操作。

而不是这样,设置如下链接:

<a href="/file_to_download_epub.php?id=<id>" download="epub_file.epub" target="_blank">Download</a>

download attribute属性将强制下载文件。

在新标签页(使用target="_blank")中打开它将确保该文件位于新标签页中,以防download属性不受支持。

如果用户有插件/插件来查看epub文件,它将使用此方法显示在浏览器中。

在你的PHP上,你必须把这些行:

header('Content-Description: File Transfer');
header('Content-Type: application/epub+zip');//important!
header('Content-Disposition: attachment; filename=file.epub');//also important

你几乎是对的!这将告诉浏览器开始下载。

跳过所有Javascript,使用普通的<a>属性,并为自己完成所有工作。