user1572993
user1572993

Reputation: 11

Force docx file download via PHP corrupt

I know there are a lot of mentions of this but I have tried all the suggestions and nothing seems to work.

I have this script to force download files, but when using docx formats it downloads ok but then says the file is corrupt. However word does manage to open it ok.

Does anyone know why the docx would keep saying there corrupt. I have double checked them by ftp them down from the server and they are fine and open first time.

$documentDir = '/home/';
$file = $_GET['d'];
$fileLocation = $documentDir.$file;
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($fileLocation)); 
header('Content-disposition: attachment; filename="'.$file.'"');
readfile($fileLocation);
exit(0);

Upvotes: 1

Views: 2498

Answers (2)

edi9999
edi9999

Reputation: 20554

This script works for me.

Are you sure that all your documents are situated in /home/ ?

Don't you mean the relative path home/

If this still doesn't work, can you please tell what's the size of the downloaded docx, and open that file in a text editor like sublime_text, the error will probably be written in there.

Upvotes: 2

Galadai
Galadai

Reputation: 77

You could try modifying disposition line and adding encoding line to ensure encoding is done correctly.

header ("Content-Disposition: attachment; filename*=UTF-8'en'$file");
header('Content-Transfer-Encoding: binary');

Upvotes: 0

Related Questions