我正在使用wordpress创建一个转到文件附件ID的“下载”弹出窗口。复杂性并不重要,但基本上我在一个带有“下载”链接的新窗口中打开它。
文件类型将是mp3,因此我不希望浏览器转到mp3并尝试使用嵌入式quicktime播放器或类似的东西,我只是想让它强行下载。
有没有比使用php header()类型的东西更简单的方法呢?
我的'下载'弹出窗口中有一大堆代码,它加载WP环境,提取一些附件网址,更改页面标题等,所以它不像php文件可以是空白的,没有空格除外只是header()代码(我知道它不能在文件中有任何其他东西才能工作)。
以下是提供“下载”链接的弹出窗口的代码。总结一下我的问题,如何在下面的代码中创建下载链接只需下载mp3而不是尝试将其加载到浏览器中。
作为旁注,有一种方法可以在下载开始后自动关闭窗口吗?这不是什么大不了的事,因为我有一个非常好的关闭窗口。
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
$mix_id = $_GET['m'];
$track_id = $_GET['t'];
$mix_attachment_id = get_post_meta( $mix_id, 'zip', true );
$mix_attachment_url = wp_get_attachment_url( $mix_attachment_id );
// Update download count
$downloads = get_post_meta($mix_id, 'downloads', true);
$downloads = (int)$downloads + 1;
update_post_meta($mix_id, 'downloads', $downloads );
// Determine if track or mix download
if ($track_id == '') {
// Mixtape Play
$title = get_the_title($mix_id);
$link_text = "Download Mixtape";
$download_link = wp_get_attachment_url( $mix_attachment_id );
} else {
// Track Download
$title = get_the_title($track_id);
$track_attachment_id = get_post_meta($track_id, 'mp3', true);
$download_link = wp_get_attachment_url( $track_attachment_id );
$link_text = "Download Track";
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
<style type="text/css">
body {
text-align: center;
margin-top: 40px;
font-family: arial, sans-serif;
}
a.download {
background: #1B62A0;
font-size: 28px;
padding: 10px 20px;
color: #fff;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
text-decoration: none;
display: inline;
}
a.download:hover {
background: #5DD8F0;
}
a.close {
text-align: center;
margin-top: 20px;
display: block;
}
</style>
</head>
<body>
<a class="download" href="<?php echo $download_link; ?>"><?php echo $link_text; ?></a>
<a href="#" class="close" onclick="javascript:window.close(); return false;">Close Window</a>
</body>
</html>
答案 0 :(得分:2)
您需要指定几个PHP标头以提示用户下载文件:http://php.net/manual/en/function.header.php#example-4064。
基本上,标题和readfile()
的组合以及退出后告诉浏览器将文件作为下载提供,而不是将其显示为网页。
答案 1 :(得分:1)
您必须使用header()
,或者必须直接链接到该文件。您可以通过简单地添加if语句来使用单个脚本。类似的东西:
// wordpress stuff
if( !empty($_GET['do_download']) ) {
// headers here and output the mp3 file.
}
else {
// show the download page
}
您必须确保的主要事项是在致电header()
之前不输出任何内容(包括空行!)
注意在我的示例中,我直接访问了$ _GET。我相信有一个Wordpress特定的方式来访问它,你应该使用它(我只是不知道它是什么)
答案 2 :(得分:1)
我做过类似的事情,虽然不在WP内。我不明白为什么你不能在发送任何标题之前在页面顶部执行Header()逻辑?
下面的内容应该有效,只需将其放在页面顶部
即可$s_filename = $_GET['s'];
// SAVE FILE
if( !empty( $s_filename ) )
{
if( file_exists( $s_filename ) )
{
$fsize = filesize( $s_filename );
// SET HEADERS
header("Content-Disposition: attachment; filename = " . $s_filename);
header("Content-Type: application/octet-stream");
header("Content-Length: " . $fsize);
header("Cache-Control: private");
readfile( $s_filename );
$result = "Your file download will begin shortly.<br />\r\n";
}
}
else
{
$result = "Error: File not found.<br />\r\n";
}