I am trying to get the code below to save to a file location on the server, can anyone help. Currently it just downloads via browser but i want to run a cron and save in a file location.
<?php
// Connection
$conn=mysql_connect('**','**','**');
$db=mysql_select_db('SHP',$conn);
$filename = "stockbook.xls"; // File Name
// Download file
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
file_put_contents("http://kempfenterprises.com/dash/reports/stockbook.xls");
$user_query = mysql_query("select * from troy.stockbook ");
// Write data to file
$flag = false;
while ($row = mysql_fetch_assoc($user_query)) {
if (!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
echo implode("\t", array_values($row)) . "\r\n";
}
?>
Thanks in advance
You can use file_put_contents
to write file in server.
<?php
$conn=mysql_connect('**','**','**');
$db=mysql_select_db('SHP',$conn);
$sql = "select * from troy.stockbook";
$qur = mysql_query($sql);
// Enable to download this file
$filename = "stockbook.csv";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv");
$display = fopen("php://output", 'w');
$flag = false;
while($row = mysql_fetch_assoc($qur)) {
if(!$flag) {
// display field/column names as first row
fputcsv($display, array_keys($row), ",", '"');
$flag = true;
}
fputcsv($display, array_values($row), ",", '"');
}
fclose($display);
?>
this is using fopen how can i now add a location where i want to save it
<?php
$conn = mysql_connect('**', '**', '**');
$db = mysql_select_db('SHP', $conn);
$sql = "select * from troy.stockbook";
$qur = mysql_query($sql);
// Enable to download this file
$filename = "stockbook.csv";
$display = fopen("php://output", 'w');
$data = "";
$flag = false;
while ($row = mysql_fetch_assoc($qur)) {
if (!$flag) {
// display field/column names as first row
$data.=implode("\t", array_keys($row)) . "\n";
$flag = true;
}
$data.=implode("\t", array_values($row)) . "\n";
}
file_put_contents($filename, $data);
exit();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.