Page 1 of 1

When .zip file downloading, extension comes to .zip.html

Posted: Tue Sep 03, 2013 12:11 am
by tens_k
Hello everyone,
I would like to know how resolve this matter.

When I click zip-file download link,
download will begin,but extension comes to .zip.html.
This is only in safari(6.0.5).

The code is,

Code: Select all

if (isset($_GET['download'])) {
  doZipDownload(wp_kses($_GET['download'],array()));
}
function doZipDownload($post_id){
	if ( !(( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ){
		wp_die( $uploads['error'] );
	}
	$upload_video_attachment_id = get_post_meta($post_id, 'upload_video_attachment_id',true);
	if(!empty($upload_video_attachment_id)){
		$file_path = get_attached_file( $upload_video_attachment_id ) ; 
	  	$file_name = basename($file_path);
	}
  	$zip_base = $uploads['basedir'];
  	$zipPath = $zip_base.'/'.$file_name.'.zip';

	try {
		$zip = new ZipArchive();
		$rtn = $zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
		if ($rtn !== true) {
			wp_die( 'ERROR'.$rtn );
		}
		$zip->addFile($file_path, $file_name);
		$zip->close();

		//	header('Content-Type: application/zip');
		//	header('Content-Type: application/octet-stream');
		$zipName = mb_convert_encoding($post_id.'.zip', 'UTF-8');
		header('Content-Disposition: attachment; filename="' . $zipName.'"');
		header('Content-Length: '.filesize($zipPath));
		header('Content-Type: application/octet-stream');
		readfile($zipPath);
		unlink($zipPath);
	} catch ( Exception $e ) {
		wp_die( $e->getMessage()) ;
	}
}

Those code fired when I click html link,
[text]
<a href="http://your-domain/?download=XXX">Download this file</a>
[/text]

In Safari ,Donwload file comes to XXX.zip.html.
In Firefox, download file comes to XXX.zip.

Apache/2.2.25
PHP 5.4.17

Why extention .zip change to .zip.html?
Any ideas?

Re: When .zip file downloading, extension comes to .zip.html

Posted: Tue Sep 03, 2013 9:48 am
by AbraCadaver
Not sure but try adding:

Code: Select all

header('Content-Transfer-Encoding: binary');

Re: When .zip file downloading, extension comes to .zip.html

Posted: Wed Sep 04, 2013 12:25 am
by tens_k
Dear AbraCadaver,
thank you for reply.

I tried your giving code.
But download file is XXX.zip.html too.

I would like to know any idea ..

Re: When .zip file downloading, extension comes to .zip.html

Posted: Thu Jan 09, 2014 11:57 pm
by tens_k
Hello,guys.

I solved this problem by myself.
I add one line like below,
(see a line before catch)

Code: Select all

                //... omitted ...
                readfile($zipPath);
                unlink($zipPath);
                exit;//add this line
        } catch ( Exception $e ) {
                //... omitted ...
thanks for your cooperation.