将 CSV 导出保存到服务器
Save CSV Export to Server
我有下面的代码,当在 WordPress 管理面板中按下按钮时触发。这将打开一个名为 'ss-stock-export.csv' 的 csv 文件。
如何将此文件保存到服务器的 wp-content/uploads/exports/
上传目录中?我尝试使用 file_put_contents
但这似乎无法正常工作。 CSV 文件出现在正确的位置,但它是空白的。可能是我的 $output
?
有问题
<?php
function generate_stock_report_csv() {
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
// set file name with current date
header('Content-Disposition: attachment; filename=ss-stock-export.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// set the column headers for the csv
$headings = array( 'sku', 'qty', 'is_in_stock' );
// output the column headings
fputcsv($output, $headings );
// get all simple products where stock is managed
// get all product variations where stock is managed
$args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_stock',
'value' => array('', false, null),
'compare' => 'NOT IN'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product = new WC_Product_Variation( $loop->post->ID );
$ss_sku = get_post_meta( $product->variation_id, 'ss_sku', true);
$stock = $product->stock;
settype($stock, "integer");
if ($stock > 0){ $stock_status = 1;} else {$stock_status = 0;}
$row = array( $ss_sku , $product->stock, $stock_status );
fputcsv($output, $row);
endwhile;
$filename = "ss-stock-export.csv"; // Trying to save file in server
file_put_contents(ABSPATH . "wp-content/uploads/exports/" . $filename, $output);
} ?>
您可以使用 wp-config.php
中定义的 ABSPATH
常量将绝对路径传递给 file_put_contents
。这样一来,您 运行 脚本的来源并不重要。
file_put_contents(ABSPATH . "wp-content/uploads/exports/" . $filename, $output);
成功了
$csv = "sku,qty,is_in_stock \n";//Column headers
$args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_stock',
'value' => array('', false, null),
'compare' => 'NOT IN'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product = new WC_Product_Variation( $loop->post->ID );
$ss_sku = get_post_meta( $product->variation_id, 'ss_sku', true);
$stock = $product->stock;
settype($stock, "integer");
if ($stock > 0){ $stock_status = 1;} else {$stock_status = 0;}
$row = array( $ss_sku , $product->stock, $stock_status );
$row_array= array($row);
foreach ($row_array as $record){
$csv.= $record[0].','.$record[1].','.$record[2]."\n"; //Append data to csv
}
endwhile;
$csv_handler = fopen ('ss-stock-export.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
file_put_contents(ABSPATH . "wp-content/uploads/exports/ss-stock-export.csv", $csv);
}
我有下面的代码,当在 WordPress 管理面板中按下按钮时触发。这将打开一个名为 'ss-stock-export.csv' 的 csv 文件。
如何将此文件保存到服务器的 wp-content/uploads/exports/
上传目录中?我尝试使用 file_put_contents
但这似乎无法正常工作。 CSV 文件出现在正确的位置,但它是空白的。可能是我的 $output
?
<?php
function generate_stock_report_csv() {
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
// set file name with current date
header('Content-Disposition: attachment; filename=ss-stock-export.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// set the column headers for the csv
$headings = array( 'sku', 'qty', 'is_in_stock' );
// output the column headings
fputcsv($output, $headings );
// get all simple products where stock is managed
// get all product variations where stock is managed
$args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_stock',
'value' => array('', false, null),
'compare' => 'NOT IN'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product = new WC_Product_Variation( $loop->post->ID );
$ss_sku = get_post_meta( $product->variation_id, 'ss_sku', true);
$stock = $product->stock;
settype($stock, "integer");
if ($stock > 0){ $stock_status = 1;} else {$stock_status = 0;}
$row = array( $ss_sku , $product->stock, $stock_status );
fputcsv($output, $row);
endwhile;
$filename = "ss-stock-export.csv"; // Trying to save file in server
file_put_contents(ABSPATH . "wp-content/uploads/exports/" . $filename, $output);
} ?>
您可以使用 wp-config.php
中定义的 ABSPATH
常量将绝对路径传递给 file_put_contents
。这样一来,您 运行 脚本的来源并不重要。
file_put_contents(ABSPATH . "wp-content/uploads/exports/" . $filename, $output);
成功了
$csv = "sku,qty,is_in_stock \n";//Column headers
$args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_stock',
'value' => array('', false, null),
'compare' => 'NOT IN'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product = new WC_Product_Variation( $loop->post->ID );
$ss_sku = get_post_meta( $product->variation_id, 'ss_sku', true);
$stock = $product->stock;
settype($stock, "integer");
if ($stock > 0){ $stock_status = 1;} else {$stock_status = 0;}
$row = array( $ss_sku , $product->stock, $stock_status );
$row_array= array($row);
foreach ($row_array as $record){
$csv.= $record[0].','.$record[1].','.$record[2]."\n"; //Append data to csv
}
endwhile;
$csv_handler = fopen ('ss-stock-export.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
file_put_contents(ABSPATH . "wp-content/uploads/exports/ss-stock-export.csv", $csv);
}