下面的代码在我的本地主机测试机器上工作,但我需要知道如何让它在外部服务器上工作。我需要能够下载 .csv 文件。有什么方法可以提示用户下载文件,还是我必须在服务器上选择一个位置?
<?php
session_start();
require ("../login.php");
$success = false;
include ("header.php");
include ("adminnav.php");
?>
<h2>Download Previews Totals</h2>
<?php
$stmt = $db->prepare
("SELECT s.short_name, p.name, t.title, SUM(sub.quantity) AS quantity
FROM store s
JOIN users u
ON s.short_name = u.store
JOIN subscriptions sub
ON u.id = sub.user
JOIN titles t
ON sub.title = t.id
JOIN publishers p
ON t.publisher = p.name
GROUP BY s.short_name, p.name, t.title
ORDER BY s.short_name, p.name, t.title");
if ($stmt->execute()) {
$rows = $stmt->fetchAll();
// Pick a filename and destination directory for the file
// Remember that the folder where you want to write the file has to be writable
$filename = "C:/Previews_Files/previews".time().".csv";
// Actually create the file
// The w+ parameter will wipe out and overwrite any existing file with the same name
$handle = fopen($filename, 'w+');
if (!$handle)
$errors[] = '<p>File failed to open';
else {
// Write the spreadsheet column titles / labels
fputcsv($handle, array('Store','Publisher', 'Title', 'Quantity'));
// Write all the user records to the spreadsheet
foreach($rows as $row)
{
fputcsv($handle, array($row['short_name'], $row['name'], $row['title'], $row['quantity']));
}
// Finish writing the file
fclose($handle);
$success = true;
}
}
else
$errors[] = '<p>There are no previews totals to display.</p>';
if ($success == true)
echo '<p>Your file has successfully been downloaded to \'C:/Previews_Files\'</p>';
else
$errors[] = '<p>Your file could not be downloaded. Please make sure the directory \'C:/Previews_Files\' has been created and is writeable.';
if (!empty($errors))
foreach($errors as $error)
echo $error;
?>
<?php
include ("footer.php");
?>