PHP脚本不会下载我的epub

I run 2 scripts, one after the other, the first one creates the ebook and stores it on the server, the second one downloads it and deletes all the files that were used to create the ebook. The scripts work perfectly, except for the download part, its not downloading the book :(

Here is the download script:

$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);
}

As you can see, ive commented out the header, because it doesnt work, i even tried 'application/octet-stream'and 'application/epub+zip' but no success.

And here is what is being outputted on the console log:

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 

Here is the html/jquery code:

        $(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>

I'm using this snippet to download pdf or doc files in my server.

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;

There are plenty of causes for this, some of them may be very hard to find.

First, your header function may be failing.

Possible solution 1:

This can be because of newlines after the closing tag.

Instead of:

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

Try:

<?php include 'my_file.php';

Possible solution 2:

Your file may be being saved in UTF8.

This won't show on the screen, but it will send 3 aditional bytes.

Try saving in ASCII, UTF7, UTF8 (without B.O.M), ANSI, windows-1252, ISO-8859-1 or ISO-8859-15.

Possible solution 3:

You are doing echo 'File does exist';.

And you don't specify a correct mime-type.

Use the mime-type application/epub+zip (source: wikipedia)

And remove that line.

Possible solution 4:

You may be outputting something else.

Try using this:

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

ob_end_clean();//discards the output

include 'your_script.php';

This will capture and discard any output from the file that includes the one that sends the new file to download.


Now, lets move to the newly added Javascript.

You are sending a POST to the file using jQuery, then you do something with the file.

Instead of that, set a link like this:

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

The download attribute attribute will force the file to download.

Opening it in a new tab (with the target="_blank") will ensure the file will be in a new tab, in case the download attribute isn't supported.

If a user has a plugin/add-on to view epub files, it will display in the browser using this method.

On your PHP, you have to put these lines:

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

You had it almost right! This will tell the browser to start a download.

Skip all that Javascript, use a plain <a> attribute and you have everything done for yourself.