Reputation: 39
I have a small problem with the php content-disposition, I kind of understand where the problem lies but I have no idea how to solve it (new to using databases). Calling this php page will result in not showing any of the echos and only showing the download box, which I intended for the "cv" only (not sure if it's working that way, because the downloadable file I receive cannot be opened)
Removing the header(content...
line will result in showing the echos, but I won't be able to download the specified file. I want it to show as a link which would download its contents when clicked.
$newEmployeeName = $_POST['name'];
$newEmployeeArea = $_POST['area'];
$newEmployeeCV = $_POST['cv'];
include('databaseConnection.php');
$result = mysql_query("SELECT * FROM participants");
while($row = mysql_fetch_array($result))
{
$download_me = $row['cv'];
header("Content-Disposition: attachment; filename=$download_me");
echo $row['name'] . " " . $row['area_of_exp'] . " " . $download_me;
echo "<br />";
}
Upvotes: 0
Views: 265
Reputation: 39439
The Content-Disposition
header will force the script to present anything echoed after it as a download. You would normally use this with reading a file from the file system, so you can offer that as a download. In your case, if you’re storing CVs on your server then you may offer them as a download as follows:
<?php
$sql = "SELECT * FROM table WHERE id = :id LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchObject();
if ($row) {
header('Content-Disposition: attachment; filename=' . $row['filename']);
readfile($uploads_dir . $row['filename']);
exit;
}
else {
die('Invalid CV requested.');
}
Obviously the above is a simplified version of the process and you will need to tweak it to fit your application, but that’s the gist of it.
Also, don’t use the mysql_
functions. They’re deprecated (as per the warning on this page). Use either PDO or the new MySQLi (MySQL improved) extension.
Upvotes: 2