TheJokerAeZ
TheJokerAeZ

Reputation: 47

How to download a file from server or local xampp? php

I would like to download a .pptx file from my site folder to downloads or getting save as pop up where user can choose folder for save. but when i call my function, its printing in console.log a the content.

i have used different codes, but no one has worked.

PK����Uy�G��-j����P����_rels/.rels���������������������JA��>Ő{w�D��^D�Md}�0���;3!�}{���B)��������l>Hʔ��uӂ��s�����{Z݃)�)��98Q��n�B3j])���TF*FU~����"�&3�:��D�Z�`�d7m{g�'�Ls��`����6�b@E��Њ�n�N�ӡ�B�ϵ]Ή���^��^(����1��HI/y�Q)

ו�����-�2�jY���9�%d����PK����Uy�G�I�����+����docProps/core.xml������������������m��j�@E�� ��-;�RL���J!��4�3������wb���R�sW���O�gq1�5-T(Z��ߞ�'�DM�f���e��~G�����c⬎�*� =�Ϊ�G:�7�"��3fo��y�d�ˌ���}D�j�Q�Wa�V#�+-�����E yb�A�������-..... And more.

this is my code:

        function downloadProject($file, $db)
        {
            $path = "../assets/convertables/Presentaties/"; // change the path to fit your websites document structure
            $dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $file.'.pptx'); // simple file name validation
            $dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
            $fullPath = $path.$dl_file;
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
            header("Content-Transfer-Encoding: Binary");
            header("Content-length: ".filesize($fullPath));
            header("Content-disposition: attachment; filename=\"".$dl_file."\"");
            readfile($fullPath);          
        }

EDIT: I have already checked other questions with answers, but has not worked for me!

Upvotes: 2

Views: 3652

Answers (1)

GiapLee
GiapLee

Reputation: 436

you can try this:

First you need define correct mime type/content type of download file

In your case is: application/vnd.ms-powerpoint

So code will be:

    $mime = 'application/vnd.ms-powerpoint';

   // Generate the server headers
    if( strstr( $_SERVER['HTTP_USER_AGENT'], "MSIE" ) ) //id browser is IE
    {
        header( 'Content-Type: "'. $mime .'"' );
        header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
        header( 'Expires: 0' );
        header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
        header( "Content-Transfer-Encoding: binary" );
        header( 'Pragma: public' );
        header( "Content-Length: ".filesize( $data ) );
    }
    else
    {
        header( "Pragma: public" );
        header( "Expires: 0" );
        header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
        header( "Cache-Control: private", false );
        header( "Content-Type: ".$mime, true, 200 );
        header( 'Content-Length: '.filesize( $data ) );
        header( 'Content-Disposition: attachment; filename='.$filename);
        header( "Content-Transfer-Encoding: binary" );
    }
    readfile( $data );
    exit;

You can learn more at website http://www.phpdevtips.com/snippets/force-file-download-with-correct-content-type/

Upvotes: 1

Related Questions