显示一些文本后,在php中下载文件

时间:2013-10-23 09:18:56

标签: php header download

您好我需要创建文件下载。用于下载和表单验证的填充表单以及dB插入都在同一页面中。如果表单已提交,则需要使用另存为窗口自动触发文件下载。我把代码编写为:

if(isset($_POST['submit'])){
$u=$_POST['uname'];
/*similarly getting all posted data*/
echo '<br><b>'.JText::_( 'Thank you for submitting your details...' ).'</b>';
 $db =& JFactory::getDBO();
  $query = "/*db inserting query*/";
  $db->setQuery( $query );
  $db->query();

  $filee=  basename($path); /*$path contains value form $_POST*/
   $full='http://localhost/joomla/images/uploads/'.$filee;

  ?>
 <br><b>
  <span>If the download  does not start automatically in a few seconds, <a href="<?php     echo JURI::base(); ?>/images/myfile/<?php echo $filee;?>" target="_blank"><b> Click this link</b></a></span></b>


 <?php


   header('Content-Description:File Transfer');
   header("Content-type:application/pdf");
   header("Content-type:application/zip");
   header("Content-type:image/jpeg");
   header("Content-type:image/png");
   header("Content-type:application/msword");
   header("Content-Disposition: attachment; filename=".$filee."");
   header("Cache-control: private");
   readfile($full);


   }
 else{ code for printing fill up form}

我需要在下载之前打印“感谢提交..”等消息。但是现在当我提交填写表单时,下载开始显示上面的文本。等待一段时间后我该怎样做才能下载?

1 个答案:

答案 0 :(得分:0)

你可以在php中使用sleep()来延迟执行。所以试试这个

if(isset($_POST['submit'])){
$u=$_POST['uname'];
echo '<br><b>'.JText::_( 'Thank you for submitting your details...' ).'</b>';
 $db =& JFactory::getDBO();
  $query = "/*db inserting query*/";
  $db->setQuery( $query );
  $db->query();
  $filee=  basename($path); /*$path contains value form $_POST*/
  $full='http://localhost/joomla/images/uploads/'.$filee;

  ?>
 <br><b>
  <span>If the download  does not start automatically in a few seconds, <a href="<?php     echo JURI::base(); ?>/images/myfile/<?php echo $filee;?>" target="_blank"><b> Click this link</b></a></span></b>


 <?php

   sleep(10);//Will delay for 10 seconds

   header('Content-Description:File Transfer');
   header("Content-type:application/pdf");
   header("Content-type:application/zip");
   header("Content-type:image/jpeg");
   header("Content-type:image/png");
   header("Content-type:application/msword");
   header("Content-Disposition: attachment; filename=".$filee."");
   header("Cache-control: private");
   readfile($full);


   }
 else{ code for printing fill up form}