2

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!

2
  • How can I down vote your comment.... Commented Dec 21, 2015 at 9:00
  • There is no way @TheJokerAez, i will remove this dont worry Commented Dec 21, 2015 at 9:05

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

5 Comments

where is the download file going? my ajax request sends succes but i can't see any download.
oh. you are using ajax... so i have idea: you can use ajax to open new browser window (or tab) with filled download url or use document.location(download_url);
i am sending the id of selected row name, and in php i am getting this name with $_post. I work with classes and functions
could you give me a little example?
Do you have download url from your code ? if have then you try past it on web address -> enter and see what will happen (dialog save file show up or not)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.