关于在数据存储在数据库中后点击提交按钮强制下载pdf文件


Regarding force download of a pdf file on clicking submit button after data is stored in database

当点击提交按钮时,我使用以下代码将数据存储在wordpress数据库中,现在代码通过将数据提交到wordpress数据库来工作。

   if ($_POST['subscribe_btn']) 
    {
    require("connect.php");
    $user = $_POST['user'];
    $email = $_POST['email'];
    if($user) 
    { 
    if(preg_match("/^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z]{2,3})$/i", $email)){
    $query=mysql_query("SELECT * FROM wp_newslettter WHERE email='$email'") or exit(mysql_error());
    $numrows= mysql_num_rows($query);
   if($numrows == 0 )
    {
    mysql_query("INSERT INTO wp_newslettter (`name`,`email`) VALUES ('$user','$email')")or exit(mysql_error());
    }
    else 
    $errmsg = "email address already exists";
    }
    else
    $errmsg = "Please enter a valid email address";
   } 
    else 
   $errmsg = "Please enter your username";
} 

我现在想做的是,一旦数据被保存到数据库中,我想强制下载一个存储在我的主题中的pdf,我真的是wordpress的新手,无法访问文件路径,我已经完成了以下代码,但没有下载文件,请提供任何帮助,我将不胜感激,我的表单操作如下action='$_SERVER[REQUEST_URI]'

/**数据提交后下载pdf的代码*/

$a =  bloginfo('template_url');
$file= $a . "/pdf/default.pdf";
$path_parts = pathinfo($file);
$ext = strtolower($path_parts["extension"]); 
switch ($ext) {
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": 
  case "docx":  $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
} 
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-type:  $ctype"); 
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate , post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;

我无法通过执行以下代码读取文件

如果文件在您的媒体库中,请尝试查看它并查看浏览器指向的URL。这是您在提交数据后需要使用header('Location: http://www.yoursite.com/wp-content/uploads/2013/02/22/file.pdf');将用户带到的URL。

函数DisplayFiles(){

$results = GetFiles(); // Function that gets files available for download 
$row = mysql_fetch_assoc( $results ); // Get first row 
echo "    <select name='files'>'n"; 
while ( $row ) { 
    echo        "<option value = '" . $row['EightDigitValue'] . "'>" .$row['FileName']. "</option>'n"; 
    $row = mysql_fetch_assoc( $results ); 
} 
echo "    </select>'n"; 

}