• Running version 0.2.5 of plugin with the Custom Roles (not really used) and Custom Taxonomies addons (both with version number manually tweaked to read the proper version 0.2.5).

    We are using on an Intranet to display mainly PDFs – meeting minutes and other such historical documents.

    Wanted files to display in the browser, if possible, instead of the files being automatically downloaded. The solution ended up being easier than I thought, with the most difficult work already being done on file upload.

    In the file /wp-content/plugins/document-repository/document-repository.php replace lines 294-309 with the following:

    // serve it up
    	ob_end_clean();
    	$filename = rawurlencode( $this->base_name( $document ) );
    	$mime_type = get_post_mime_type( $attachment->attachment_id );
    	if( !empty( $attachment->post_mime_type ) ) {
    		$mime_type = $attachment->post_mime_type;
    	}
    	if ( empty( $mime_type ) ) {
    		header( 'Content-Description: File Transfer' );
    		header( 'Content-Type: application/octet-stream' );
    		header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
    	} else {
    		header( 'Content-Type: ' . $mime_type );
    		header( 'Content-Disposition: inline; filename="' . $filename . '"' );
    	}
    	header( 'Content-Transfer-Encoding: binary' );
    //	if ( false === strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) )
    		header( 'Content-Length: ' . filesize( $document_file ) );
    	readfile( $document_file );
    	ob_start();
    	exit;

    Basically, if there is a MIME type specified in the attachment post, I use that and use an “inline” disposition. Otherwise, it does the old download method.

    I completely disabled the output buffering and restart it after the “readfile” pass through is done, so the two “flush” actions in the original are not needed. This avoids any buffer issues. I send the “Content-Length” parameter for all servers – if this is a problem, uncomment the preceding line. I could find no documentation on why this may have been needed. My servers all run Ubuntu, so I couldn’t test on IIS.

    Another thing I have not tested in the “rawurlencode” that I added to the filename that gets included in the header. I also added quotation marks around the filename.

    One enhancement would be to make the action selectable, so you could provide “download” and “view” options. That was complexity we do not need, so I did not provide.

    If anyone finds any issues with this patch, please provide feedback! Ron, if you want to put this code into the plugin, feel free to do so – it’s my way of saying “thanks”

    https://wordpress.org/plugins/document-repository/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘View instead of download’ is closed to new replies.