Jump to content

[NEED HELP!]when i use Header to init download


qszhc123

Recommended Posts

go on with the title, i mean, when i click the download button and it starts downloading, i can't refresh the page or go to other adress of my site,  it will really  take a long time. the header information is something like this  (except some judgement)

    header("Content-Disposition: attachment; filename=" . $name);   

    header("Content-Type: application/force-download");

    header("Content-Type: application/octet-stream");

    header("Content-Type: application/download");

    header("Content-Description: File Transfer");           

    header("Content-Length: " . filesize($adress));

    flush(); // this doesn't really matter.

 

    $fp = fopen($adress, "r");

    while (!feof($fp))

    {

      echo fread($fp, 65536);

      flush(); // this is essential for large downloads

    } 

    fclose($fp);

 

it takes me days but i cant find any way...thanks a lot!!! thanks a lot!!! thanks a lot!!!

Link to comment
Share on other sites

the link that opens that page, make sure the target is target="_blank"

also you could do this

header("Content-Disposition: attachment; filename=" . $name);   
    header("Content-Type: application/force-download"); //no point having multiple contents types
    header("Content-Description: File Transfer");             
    header("Content-Length: " . filesize($adress));
    readfile($adress);
//or
//include $adress;
    exit();

Link to comment
Share on other sites

_new only opens 1 windows where as _blank always opens a new window

 

example

From main.html if you click both the links (below) your get 2 new windows

<a href="123.html" target="_blank">123</a>

<a href="456.html" target="_blank">456</a>

 

where as if

From main.html if you click both the links (below) your get 1 new windows (first 123 then thats replaced with 456)

<a href="123.html" target="_new">123</a>

<a href="456.html" target="_new">456/a>

 

Hope that clears that up :P

 

also can you please click 'Topic Solved' (button at bottom left)

Link to comment
Share on other sites

thanks for explaination~~ :) 

 

but sorry sir, the problem seems remains  :<

 

the first time i try i was ok but afterwards mostly failed

 

so now i should do something with Header? if don't bother you , could you give me some advice in details?

 

thanks ~~

 

 

Link to comment
Share on other sites

So let me check,

Your on a page with a download link, you click the link and another page opens (maybe only flash's open),

and the page (with the download link on) hangs!

 

Can you post the code that handles the download page, as i believe you may have some data above it that's outputting thus stopping the content type header and as such filling up the browsers page buffer!

Link to comment
Share on other sites

include ($adress) do work, but it's large files download ,if i use include ($adress) directly,APACHE will tells me like followings:

[04-Feb-2012 15:00:23] PHP Fatal error:  Allowed memory size of 15728640 bytes exhausted (tried to allocate 6291456 bytes) in D:\wamp\www\uploads\2012-01-28\phpE8D9.tmp on line 48397

the memory limit is 15M, and i don't wanna change it because maybe many users

 

the complete file is as following, it contains some Chinese that may be messy code in you computer,i'm sorry for it

<?php
  //---------------------------
  //先判断是否已经完成本页面的逻辑判断
  session_start();
  if (!isset($_SESSION["file_pre_".$_GET["id"]])){
  //---------------------------
  //引入网站配置文件
  include_once 'incs/incs.php';
  //---------------------------
  //echo "进入逻辑判断.<br />";
  if ($right->getlevel()<$site->get_download_right()){
$error->set_page_title('下载提示');
  	$error->stop('由于权限限制,您不能下载该文件!');
  }
  
  $page->setpageid();
  $id=$page->getpageid();
  $query=$mysql->select('*', '`files`', 'WHERE id='.$id.' AND deleted=0 AND folder=0');
  $row=$mysql->fetch_array($query);

  if (!$row){
$error->set_page_title('下载提示');
  	$error->stop('您下载的文件不存在或已被管理员删除!');
  }
  if(!$_SESSION["file_".$_GET["id"]]==1){
$error->set_page_title('下载提示');
  	$error->stop('请勿盗链本站资源!');
  
  }  
  if(!file_exists('d:/wamp/www'.$row['adress'])){
$error->set_page_title('下载提示');
  	$error->stop('您下载的文件不存在或已被管理员删除!');  	
  }

  $mysql->update('`files`', 'downtimes = downtimes + 1', 'WHERE id='.$id);
  
  $_SESSION["file_adress_".$_GET["id"]]=$row["adress"];
  $_SESSION["file_name_".$_GET["id"]]=$row["name"];
  $_SESSION["file_pre_".$_GET["id"]]=1;
  //echo "判断完成<br>";

} 
  if(isset($_SESSION["file_pre_".$_GET["id"]])) {
  
    $adress="D:/wamp/www".$_SESSION["file_adress_".$_GET["id"]];
    $name=rawurlencode($_SESSION["file_name_".$_GET["id"]]);
    
    header("Content-Disposition: attachment; filename=" . $name);    
    header("Content-Type: application/force-download");
   // header("Content-Type: application/octet-stream");
   // header("Content-Type: application/download");
    header("Content-Description: File Transfer");             
    header("Content-Length: " . filesize($adress));
    flush(); // this doesn't really matter.

    $fp = fopen($adress, "r"); 
    //include ($adress);
    while (!feof($fp))
    {
      
      echo fread($fp, 65536); 
      flush(); // this is essential for large downloads
    }  
    fclose($fp); 
    exit;
    

  }

 

 

the link to download.php:

<a href="download.php?id={$file.id}" target="_blank">
            	<img id="files_main_inform_downbutton" src="../imgs/files_download_button.png" width="150" height="50">
            </a>

{$file.id} is something of smarty

 

Thanks~~

Link to comment
Share on other sites

i use session because the sql "update download times" will run many times without it,

session help me make sure the sql have updated,

and others is for link protection,  because files are large , i don't want others download them form other sites

 

: )

Link to comment
Share on other sites

When your using sessions, each page that calles session_start will acquire a lock on the session's data file.  As long as the script is running the session cannot be accessed by any other files until the lock is released which happens when the session is written out at the end of the script.

 

Your download script is locking the session file, but it will not release it until the script ends which is when the download is complete.  This means no other pages on your site which use the session will work for this duration of time.

 

You can close the session manually using session_write_close in your script.  Call this function just before you output your headers and the file data.  That will release the session lock so the rest of the site will work while the file downloads.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.