Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <p>Pass in the query-param "file" that is a numeric value of the attachment ID in WordPress<br />
- Example: http://www.mysite.com/loader/?file=12804</p>
- <?php
- // load file pass-through
- // include the WP environment
- include_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
- // IF USER IS NOT LOGGED IN, FORCE THE LOGIN
- if( !is_user_logged_in() ):
- get_header(); ?>
- <section id="content">
- <div class="layout">
- <aside id="sidebar">
- <?php get_sidebar(); ?>
- </aside>
- <article id="main">
- <h1>Login Required to View this Resource</h1>
- <?php wp_login_form(); ?>
- </article>
- </div>
- </section>
- <?php
- get_footer();
- else:
- // set the incoming FILE ID to a var, and then look up the file
- // if the param is NOT SET or is NOT A NUMBER then display an error
- $fileID = ( !is_numeric( $_GET['file'] ) || !isset( $_GET['file'] ) ? null : $_GET['file'] );
- // get the file object
- $fileObj = get_post( $fileID );
- // display error if ID ENDS UP BEING NULL
- if( is_null( $fileObj ) ):
- echo 'There was an error loading the file. Please check the URL and try again. If this error persists, please contact the website administrator with the TITLE and URL of this file. Thank you!';
- else:
- $file = array();
- $file['location'] = $_SERVER['DOCUMENT_ROOT'] . str_replace( site_url(), '', $fileObj->guid );
- $file['url_parts'] = explode( '/', $file['location'] );
- $file['save_as'] = end( $file['url_parts'] );
- $file['mimetype'] = $fileObj->post_mime_type;
- header("Content-Type: application/octet-stream");
- header("Content-Transfer-Encoding: Binary");
- header("Content-disposition: attachment; filename=\"".$file['save_as']."\"");
- echo readfile( $file['location'] );
- endif;
- endif;
- ?>
Add Comment
Please, Sign In to add comment