SO网友:Paul G.
在代码中使用以下内容类型标题:
header( "Content-type: application/octet-stream" );
如果这不起作用——我们成功地做到了这一点,尽管可能性不大,但这可能与事情的顺序有关。按以下顺序尝试:
Content-type
Content-disposition
Content-Transfer-Encoding
Content-Length
正如在另一个答案中所提到的,在将输入直接用于数据库查询之前,请始终对其进行清理。
已编辑:还添加以确保没有缓存(已执行from here)-
header(\'Connection: Keep-Alive\');
header(\'Expires: 0\');
header(\'Cache-Control: must-revalidate, post-check=0, pre-check=0\');
header(\'Pragma: public\');
已编辑:将代码包装到一个函数中以在“init”处运行,这样它就不会输出到主题内容中。
add_action(
\'init\',
function () {
global $wpdb;
$fid = sanitize_text_field( $_GET[ \'fid\' ] );
$sel = $wpdb->get_results( "SELECT * FROM uploads WHERE id = $fid" );
foreach ( $sel as $head ) {
$name = empty( $head->name ) ? \'myfile.name\' : $head->name;
$size = empty( $head->size ) ? strlen( $head->content ) : $head->size;
header( \'Content-Type: application/octet-stream\' );
header( "Content-Disposition: attachment; filename=$name" );
header( \'Content-Transfer-Encoding: Binary\' );
header( "Content-Length: $size" );
header( \'Connection: Keep-Alive\' );
header( \'Expires: 0\' );
header( \'Cache-Control: must-revalidate, post-check=0, pre-check=0\' );
header( \'Pragma: public\' );
echo $head->content;
exit();
}
}
);