douyakan8924
2012-12-04 10:37 阅读 36
已采纳

pdf文件下载链接无法在php中工作[关闭]

I've been using this site to look for answers but I can't find any that may help so I decided to create an account and ask myself.

Here is a link to the website that I'm working on http://www.h-elpsystems.co.uk/index.php. It's a database driven website which allows users to download files. Now I was working on one page of the website (as far as I'm aware I didn't touch any of the code that used the download code) and now all of a sudden the downloads don't work.

The downloads are located on the "employment law page" at the bottom. When you click on the link to download a file it just sends the user to the same page only with a # in the URL at the end.

I'm not sure which bit of code to upload (I'm new to php) but if someone will let me know which is needed I'll upload it.

Thanks for any help in advance.

PS here is the code that I'm using for the download. (note I didn't create the code, I just edited a page or two and the downloads seemed to have stopped working for some reason.)

<?
// Define the path to file
$file = "membersfiles/".$_GET['ID'];

if(!file)
{
 // File doesn't exist, output error
 die('file not found');
}
else
{
 // Set headers
 header("Cache-Control: public");
 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=$file");
 header("Content-Type: application/zip");
 header("Content-Transfer-Encoding: binary");

 // Read the file from disk
 readfile($file);
 }
 ?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Download file</title>
 </head>

 <body>
 </body>
 </html>

Also below is the code for the employment page. (Sorry I'm new to this forum so didn't really know what to include in the question)

<?
session_start;
include("../Functions.inc");
$ID=PageID(substr(dirname($_SERVER['PHP_SELF']), 1, strlen(dirname($_SERVER['PHP_SELF']))));

?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><? echo PageTitle($ID);?></title>
<meta name="keywords" content="<? echo PageKeywords($ID);?>" />
<meta name="description" content="<? echo PageDescription($ID);?>" />
<meta name="robots" content="index,follow" />


<link href="../mainstyles.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>

<div id="wrapper">

<div id="leftcolumn"><div id="mainnav"><img src="../media/logo.gif" alt="H-elp Systems" /><? include("../Left-Links.inc"); ?>
<a href="mailto:info@h-elpsystems.com" id="emailbutton" ><span>click here to email us</span></a>
<img src="../media/navfooter.gif" alt="Help Systems" /></div>
</div>


<div id="centercolumn">
<div id="topnav">
<? include("../Top-Links.inc"); ?>
</div>

<div id="content">
<img src="../media/headers/<? echo PageTitle($ID);?>.jpg" alt="" />
<img src="../media/splashtext/<? echo PageTitle($ID);?>.gif" alt="When it come to employment you need h-elp systems" /><a href="#" id="phonelink" ><span>0121 635 5870</span></a>
<img src="../media/hr450.gif" alt="Help Systems" />
<div id="contentcopy">
<h1><? echo PageSplash($ID);?></h1>
<? echo PageContent($ID);?>

<?
    $query = "SELECT * FROM `Files` WHERE  `Employment`=1";    
    $result = mysql_query($query) or die ("Failed To get member files!"); 

    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

        print("<img src=\"../media/hr450.gif\"/><br><a href=\"#\" onclick=\"javascript:downloadwindow('../Download.php?ID=".$line['URL']."');\">".$line['URL']."</a>");

    }
?>
</div>
<img src="../media/hr450.gif" alt="Help Systems" />

<div id="footer">
<p>RJS Management Services Ltd   Concorde House   Trinity Park   Solihull   B37 7UQ<br />
Tel.+44 (0)121 635 870   Email. <a href="mailto:action@h-elpsystems.com">action@h-elpsystems.com</a></p>
</div>
</div>

</div>
<div id="rightcolumn">

<? 
$query = "SELECT * FROM `News` ORDER BY `Date` DESC limit 0, 4";    
$result = mysql_query($query) or die ("Failed To get latest news!"); 

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $ShortContent=substr($line['Content'],0,175);
    print("<h2>".$line['Title']."</h2>".$ShortContent."...<p class=\"small\">".$line['Date']."</p><a href=\"../News.php?Article=".$line['ID']."\" class=\"clickformore\" ><span>click here for more</span></a><img src=\"../media/hr.gif\" alt=\"H-elp\" />");

} 
 ?>

 </div>


 </div>
 </body>
 </html>


 <?
 //Close any open connetions
 mysql_close;
 ?>

edit: I've tried everyone's answers and they seem to work in some form but for some reason when i upload the changed code to the server it seems to bring up a blank page when ever I click on the employment law page. Now I don't know if I'm saving it wrong or uploading it wrong or if something else is wrong.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    douxin2002 douxin2002 2012-12-04 10:43

    The first thing to do, is add the code from the employment page that produces the download links. This will give us an idea of where the download is coming from in the website.

    Right now it looks like the problem is the download link itself; all the links simply point to the employment page with a hashtag but no variable after the hashtag. So, unless a form is supposed to submit the data onclick (which is unlikely in the extreme), then the download URLs are incorrect.

    So add the code from the employment page that displays the download links and we can go from there.


    The problem is definitely the link on the employment page. I suspect that a file being included has been moved or modified.

    Check that include("../Functions.inc"); is still in that path relative to the employment page (i.e. one folder up from the employment page).

    Next thing to do if that's not the problem, in my opinion, would be to modify the code. It's not really necessary to be using seperate javascript code to open the download in a new window.

    So change this line on the employment page:

    print("<img src=\"../media/hr450.gif\"/><br><a href=\"#\" onclick=\"javascript:downloadwindow('../Download.php?ID=".$line['URL']."');\">".$line['URL']."</a>");
    

    To this:

    print("<img src=\"../media/hr450.gif\"/><br><a href=\"../Download.php?ID=".$line['URL']."\" target=\"_blank\">".$line['URL']."</a>");
    

    I hope this helps.

    点赞 评论 复制链接分享
  • dou12352 dou12352 2012-12-04 10:48

    Take a look at the source on affected links:

    <a href="#" onclick="javascript:downloadwindow('../Download.php?ID=Adoption Leave_Employee Guidelines_Nov 2012.pdf');">Adoption Leave_Employee Guidelines_Nov 2012.pdf</a>
    

    You are using Javascript to open a download window, but my Javascript console in Firefox tells me this:

    Error: ReferenceError: downloadwindow is not defined
    

    You either forgot to upload a missing Javascript (.js) file for that system or included it wrong.

    Also, when I try to download by opening the URL directly at Download.php?ID=....pdf it leads to a corrupt archive file, but this is another case I guess.

    点赞 评论 复制链接分享
  • doujiang2812 doujiang2812 2012-12-04 10:52

    You are calling a Javascript function that is not included in the document, so obviously is not working. However one of the values passed to the function is the url of the file (it's a PHP with GET parameters) and it's correct. If you don't know exactly how to do, do the following for every link:

    instead of

    <a href="#" onclick="javascript:downloadwindow('../Download.php?ID=Adoption Leave_Employee Guidelines_Nov 2012.pdf');">Adoption Leave_Employee Guidelines_Nov 2012.pdf</a>
    

    try this:

    <a href="../Download.php?ID=Adoption Leave_Employee Guidelines_Nov 2012.pdf">Adoption Leave_Employee Guidelines_Nov 2012.pdf</a>
    

    i.e., take the URL from within the onclick statement and put it in the href. First of all try to modify only the first link and test if it works, then do the same procedure for every single link. It'll take a while but it should work.

    点赞 评论 复制链接分享
  • doulingzhuang3079 doulingzhuang3079 2012-12-04 11:17

    Go from @mastazi and add an iframe and target the href to that, that way you stay on the page:

    <a href="download.php?pdf=..." target="downloadFrame">
    <a href="...
    <iframe style="display:none;" name="downloadFrame"></iframe>
    
    点赞 评论 复制链接分享

相关推荐