bp_document_download_file( $attachment_id, $type = 'document' )
Description
Source
File: bp-templates/bp-nouveau/includes/document/functions.php
function bp_document_download_file( $attachment_id, $type = 'document' ) { // Add action to prevent issues in IE. add_action( 'nocache_headers', 'bp_document_ie_nocache_headers_fix' ); if ( 'document' === $type ) { $the_file = wp_get_attachment_url( $attachment_id ); if ( ! $the_file ) { return; } // clean the file url. $file_url = stripslashes( trim( $the_file ) ); // get filename. $file_name = basename( $the_file ); // get file extension. $file_extension = pathinfo( $file_name ); // security check. $file_name_lower = strtolower( $file_url ); // Get all allowed document extensions. $all_extensions = bp_document_extensions_list(); $allowed_for_download = array(); $allowed_file_type_with_mime_type = array(); foreach ( $all_extensions as $extension ) { if ( isset( $extension['is_active'] ) && true === (bool) $extension['is_active'] ) { $extension_name = ltrim( $extension['extension'], '.' ); $allowed_for_download[] = $extension_name; $allowed_file_type_with_mime_type[ $extension_name ] = $extension['mime_type']; } } $whitelist = apply_filters( 'bp_document_download_file_allowed_file_types', $allowed_for_download ); $file_arr = explode( '.', $file_name_lower ); $needle = end( $file_arr ); if ( ! in_array( $needle, $whitelist ) ) { exit( 'Invalid file!' ); } $file_new_name = $file_name; $content_type = isset( $allowed_file_type_with_mime_type[ $file_extension['extension'] ] ) ? $allowed_file_type_with_mime_type[ $file_extension['extension'] ] : ''; $content_type = apply_filters( 'bp_document_download_file_content_type', $content_type, $file_extension['extension'] ); bp_document_download_file_force( $the_file, $file_name ); } else { // Get folder object. $folder = folders_get_folder( $attachment_id ); // Get Upload directory. $upload = wp_upload_dir(); $upload_dir = $upload['basedir']; // Create temp folder. $upload_dir = $upload_dir . '/' . $folder->user_id . '-download-folder-' . time(); // If folder not exists then create. if ( ! is_dir( $upload_dir ) ) { // Create temp folder. wp_mkdir_p( $upload_dir ); chmod( $upload_dir, 0777 ); // Create given main parent folder. $parent_folder = $upload_dir . '/' . $folder->title; wp_mkdir_p( $parent_folder ); // Fetch all the attachments of the parent folder. $get_parent_attachments = bp_document_get_folder_attachment_ids( $folder->id ); if ( ! empty( $get_parent_attachments ) ) { foreach ( $get_parent_attachments as $attachment ) { $the_file = get_attached_file( $attachment->attachment_id ); $file_name = basename( $the_file ); copy( $the_file, $parent_folder . '/' . $file_name ); } } bp_document_get_child_folders( $attachment_id, $parent_folder ); $zip_name = $upload_dir . '/' . $folder->title . '.zip'; $file_name = sanitize_file_name( $folder->title ) . '.zip'; $rootPath = realpath( "$upload_dir" ); $zip = new ZipArchive(); $zip->open( $zip_name, ZipArchive::CREATE | ZipArchive::OVERWRITE ); $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $rootPath ), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ( $files as $name => $file ) { $filePath = $file->getRealPath(); $relativePath = substr( $filePath, strlen( $rootPath ) + 1 ); if ( ! $file->isDir() ) { $zip->addFile( $filePath, $relativePath ); } else { if ( $relativePath !== false ) { $zip->addEmptyDir( $relativePath ); } } } $zip->close(); header( 'Expires: 0' ); header( 'Cache-Control: no-cache, no-store, must-revalidate' ); header( 'Cache-Control: pre-check=0, post-check=0, max-age=0', false ); header( 'Pragma: no-cache' ); header( 'Content-type: application/zip' ); header( "Content-Disposition:attachment; filename={$file_name}" ); header( 'Content-Type: application/force-download' ); readfile( "{$zip_name}" ); BP_Document::bp_document_remove_temp_directory( $upload_dir ); exit(); } } }
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.