Tuesday 15 February 2011

php - How to force download attachment -



php - How to force download attachment -

i'm trying forcefulness download zip attachment in wordpress.

$file_url = wp_get_attachment_url( $name->name ); header('content-type: application/zip'); header("content-transfer-encoding: binary"); header('content-disposition: attachment; filename="test.zip"'); header("content-length: ".filesize( $file_url )); readfile($file_url);

if echo $file_url outputs right url. file corrupted when download. if set $file_url manually same url, file not corrupted when downloading. ideas?

as @h2ooooooo stated in reply, filesize won't work url's. needs utilize get_attached_file instead of wp_get_attachment_url path.

this worked:

$file_path = get_attached_file( $name->name ); header('content-type: application/zip'); header('content-transfer-encoding: binary'); header('content-disposition: attachment; filename="test.zip"'); header('content-length: '.filesize( $file_path )); readfile($file_path);

php wordpress

No comments:

Post a Comment