2015-09-04 35 views
1

我创建了一个查询,它从数据库中提取我想要的值,并将它们放入一个数组中(var_dump显示了这个),但是没有创建文件。这已经从一大堆资源拼凑起来,以实现这一目标。在某处,我错过了一些实际创建CSV文件并强制下载的地方。从Wordpress输出查询到CSV

function wpcp_export_inventory_feed() 
{ 


$args = array(
    'showposts'  => -1, 
'offset'   => 0, 
'orderby'   => 'date', 
'order'   => 'DESC', 
'post_type'  => 'inventory', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

$posts = get_posts($args); 

$fh = fopen('wp-content/uploads/export.csv', 'w'); 

$vehicles[] = array(); 
$vehicles[] = array('Stock', 'Year', 'Make', 'Model', 'Trim', 'Auto Title', 'VIN', 'Price', 'Discount', 'MSRP', 'Mileage', 'Body Style', 'Engine', 'Transmission', 'Exterior Color', 'Interior Color', 'Fuel', 'MPG City', 'MPG Hwy'); 

while (have_posts()) : the_post(); 

    $autoID = get_the_ID(); 
    $year = get_post_meta($autoID, '_auto_year', true); 
    $make = get_post_meta($autoID, '_auto_make', true); 
    $model = get_post_meta($autoID, '_auto_model', true); 
    $trim = get_post_meta($autoID, '_auto_trim', true); 
    $autotitle = "$year $make $model $trim" ; 
    $autostock = get_post_meta($autoID, '_auto_stock', true); 
    $autovin = get_post_meta($autoID, '_auto_vin', true); 
    $autoprice = get_post_meta($autoID, '_auto_price', true); 
    $autodiscount = get_post_meta($autoID, '_auto_discount', true); 
    $automsrp = get_post_meta($autoID, '_auto_msrp', true); 
    $automileage = get_post_meta($autoID, '_auto_mileage', true); 
    $autobodystyle = get_post_meta($autoID, '_auto_body', true); 
    $autoengine = get_post_meta($autoID, '_auto_engine', true); 
    $autotrans = get_post_meta($autoID, '_auto_trans', true); 
    $autocolorext = get_post_meta($autoID, '_auto_color_ext', true); 
    $autocolorint = get_post_meta($autoID, '_auto_color_int', true); 
    $autofuel = get_post_meta($autoID, '_auto_fuel', true); 
    $autompgcity = get_post_meta($autoID, '_auto_gasmiles_city', true); 
    $autompghwy = get_post_meta($autoID, '_auto_gasmiles_hwy', true); 

    $vehicles[] = array($autostock, $year, $make, $model, $trim, $autotitle, $autovin, $autoprice, $autodiscount, $automsrp, $automileage, $autobodystyle, $autoengine, $autotrans, $autocolorext, $autocolorint, $autofuel, $autompgcity, $autompghwy); 

endwhile; 

foreach($vehicles AS $vehicle) { fputcsv($fh, $vehicle, ';'); } 
fclose($fh); 

var_dump($vehicles); 

} 

回答

0

我认为你有一个路径问题。至少你会有一个零大小的文件,如果你的逻辑的其余部分有问题。如果你扫描所有的文件夹,你可能会找到它。

你需要给路径根锚:

$fh = fopen ('/wp-content/uploads/export.csv', 'w');