如何保存 CSV 而不下载
我有这个 php 脚本
$connection = mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DATABASE, $connection);
$query = sprintf( 'SELECT * FROM something' );
$result = mysql_query($query);
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename=export.csv' );
$row = mysql_fetch_assoc( $result );
if ( $row )
{
echocsv( array_keys( $row ) );
}
while ( $row )
{
echocsv( $row );
$row = mysql_fetch_assoc( $result );
}
function echocsv( $fields )
{
$separator = '';
foreach ( $fields as $field )
{
if ( preg_match( '/\\r|\\n|,|"/', $field ) )
{
$field = '"' . str_replace( '"', '""', $field ) . '"';
}
echo $separator . $field;
$separator = ',';
}
echo "\r\n";
}
,它可以抓取所有记录并将它们保存到 csv 中......效果很好。但我需要做的是从 cron 运行这个 php 脚本并将 csv 保存到特定文件夹...
这里的任何想法都是我如何通过 cron 任务调用文件
wget -O /dev/null http://somesite.com/make_csv.php
I have this php script
$connection = mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DATABASE, $connection);
$query = sprintf( 'SELECT * FROM something' );
$result = mysql_query($query);
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename=export.csv' );
$row = mysql_fetch_assoc( $result );
if ( $row )
{
echocsv( array_keys( $row ) );
}
while ( $row )
{
echocsv( $row );
$row = mysql_fetch_assoc( $result );
}
function echocsv( $fields )
{
$separator = '';
foreach ( $fields as $field )
{
if ( preg_match( '/\\r|\\n|,|"/', $field ) )
{
$field = '"' . str_replace( '"', '""', $field ) . '"';
}
echo $separator . $field;
$separator = ',';
}
echo "\r\n";
}
which grabs all the records and saves them into a csv ....which works great. But what i need to do is run this php script from a cron and save the csv a specific folder...any ideas
here is how i am calling the file via a cron task
wget -O /dev/null http://somesite.com/make_csv.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看 fopen 和 fwrite 函数,应该可以完成这项工作。
Take a look at the fopen and fwrite functions, should do the job.