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


File downloading in php after displaying some text

嗨,我需要创建文件下载。

用于下载和表单验证的填写表单以及 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}

我需要打印"感谢您提交.."。等消息在下载之前,但是现在当我提交填写表格下载时,下载开始不显示上述文本。等待一段时间后,我应该怎么做才能进行下载?

你可以在 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}