bp_document_download_file (attachment_id美元,美元的类型=“文档”)
描述
源
文件:bp-templates / bp-nouveau / /文档/显然也包括
function bp_document_download_file($attachment_id, $type = 'document'){//添加防止IE问题的操作。Add_action ('nocache_headers', 'bp_document_ie_nocache_headers_fix');如果('document' === $type) {$the_file = wp_get_attachment_url($attachment_id);如果(!$the_file) {return;} //清除文件url。$file_url = strpslashes (trim($the_file));/ /获取文件名。$file_name = basename($the_file);//获取文件扩展名 $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(); } } }