Downloading data to a csv file from MySQL using PHP(使用 PHP 从 MySQL 下载数据到 csv 文件)
问题描述
如何指定要从数据库下载的文件?
How do I specify the file to be downloaded from the database?
main.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(这是问题所在):
download.php (which is where the problem lies):
$file = $_GET['row["cv"]'];
header('Content-Disposition: attachment; filename="'.$file.'"');
echo $file;
我希望在点击 main.php 中的链接时下载简历
I want the CV to be downloaded when the link in main.php is clicked
推荐答案
如果您的文件确实在您的数据库中,您可以通过以下方式检索它:
In the case your file is indeed in side your database you could retrieve it this way:
Main.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 &更具体的版本:
mysql & more specific version:
<?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 &更具体:
Mysql & more specific:
<?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
我建议您不要将简历存储在您的数据库中.
I advice you to not store the CV in your database.
这篇关于使用 PHP 从 MySQL 下载数据到 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP 从 MySQL 下载数据到 csv 文件
基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01