2

如何指定要从数据库下载的文件?

主.php:

    include('databaseConnection.php');

    $result = mysql_query("SELECT * FROM participants");

    while($row = mysql_fetch_array($result)){


$download_me= $row['cv'];

echo $row['name'] . " " . $row['area_of_exp'] . " " ;

echo "<a href='download.php?file=$download_me'> cv </a>";

echo "<br />";
  }

download.php (这是问题所在):

$file = $_GET['row["cv"]'];  
header('Content-Disposition: attachment; filename="'.$file.'"');
echo $file;

我希望在单击 main.php 中的链接时下载 CV

4

2 回答 2

1

如果您的文件确实在您的数据库中,您可以通过以下方式检索它:

主.php:

<?php
  $host = "hostname";
  $user = "username";
  $password = "password";
  $database = "database";

  $link = mysqli_connect($host, $user, $password, $database);
  If (!$link){
      echo ("Unable to connect to database!");
  }
  else {
  $query = "SELECT * FROM participants";    
  $result = mysqli_query($link,$query);

  while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){

echo $row['name'] . " " . $row['area_of_exp'] . " " . '<a href="download.php?file='.$row['id_participants']. '"\>'." cv </a><br />";
  }


}
mysqli_close($link);
?>

mysql & 更具体的版本:

<?PHP
      include('databaseConnection.php');


      $query = "SELECT * FROM participants";    
      $result = mysql_query($query);

      while ($row = mysql_fetch_array($result, MYSQL_BOTH)){

    echo $row['name'] . " " . $row['area_of_exp'] . " " . '<a href="download.php?file='.$row['id_participants']. '"\>'." cv </a><br />";
      }

      mysql_close($link);
    ?>

下载.php:

<?php


$id_participants = $_GET['file'];

$host = "hostname";
$user = "username";
$password = "password";
$database = "database";

$link = mysqli_connect($host, $user, $password, $database);
If (!$link){
      echo ("Unable to connect to database!");
  }
  else {
$query = "SELECT * FROM participants WHERE id_participants = $id_participants";

$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
$size = $row['File_Size'];
$type = $row['File_Type'];
$name =$row['File_Name'];
$CV = $row['cv'];
// echo $size . "". $type . " ". $name;

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $CV;

}
mysqli_close($link);

?>

Mysql & 更具体:

<?php

include('databaseConnection.php');

$id_participants = $_GET['file'];


$query = "SELECT * FROM participants WHERE id_participants = $id_participants";

$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_BOTH);
$size = $row['File_Size'];
$type = $row['File_Type'];
$name =$row['File_Name'];
$CV = $row['cv'];
// echo $size . "". $type . " ". $name;

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $CV;

}
mysql_close();

?>

数据库布局:

CREATE TABLE IF NOT EXISTS `participants` (
  `id_participants` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `area_of_exp` varchar(30) NOT NULL,
  `cv` blob NOT NULL,
  `File_Size` varchar(30) NOT NULL,
  `File_Type` varchar(30) NOT NULL,
  `File_Name` varchar(30) NOT NULL,
  PRIMARY KEY (`id_participants`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

http://www.sqlfiddle.com/#!2/91cb3/4

我建议您不要将简历存储在数据库中。

于 2013-01-19T22:50:57.660 回答
0

我希望$row['cv']变量中有一个文件名?

关于文件,你不应该回显变量(这样你只显示文件名)。

要输出文件,您应该使用readfile()。此外,在您的 download.php 文件中,您尝试检索一个不存在的 GET 变量。

例如:

$file = $_GET['file'];
header('Content-Disposition: attachment; filename="'. basename($file) . '"');
readfile($file);

您应该知道 $file 变量应该包含文件的完整路径(无论是绝对路径还是相对路径)。这就是为什么我使用basename() -函数来仅获取文件名的原因。

于 2013-01-19T20:59:52.217 回答