【问题标题】:PHP fputcsv is not working, Nor creating csvfilesPHP fputcsv 不工作,也不创建 csvfiles
【发布时间】:2016-09-17 14:09:43
【问题描述】:

我通过它将所有产品导出到 csv 文件中,

但我的 csvfile 没有创建,我可以在屏幕上看到变量输出。

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);
require_once '/var/www/html/app/Mage.php';
umask(0);

Mage::app('default');

$connection = Mage::getSingleton('core/resource')->getConnection('core_write');

define('CSV_PATH', 'var/www/html/rahul_csvfiles/'); // specify CSV file path

$csv_fileoutput = CSV_PATH . "cfd_all_products_log".date("Y-m-d H:i:s").".csv";
$csvfileoutput  = fopen($csv_fileoutput, 'a');
fputcsv($csvfileoutput, array('Entity_id','SKU','Product_Name','Product Name','Title', 'Short Description', 'Long Description', 'URL'));

$store_id = 2;
$collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter($store_id)
->addAttributeToSelect('*') 
->setPageSize(3) 
->setCurPage(1);

foreach ($collection as $product) {
  $product_id =  $product->getID();
  $sku = $product->getSKU();
  $productname =  $product->getName(); 
  $price =  (float) $product->getPrice(); 
  $description =  $product->getDescription();  
  $shortdescription = $product->getShortDescription(); 
  $url =  $product->getProductUrl();
echo $product_id. "\n";
echo $sku. "\n";
echo $productname. "\n";
echo $price. "\n";
echo $url . "\n";

fputcsv($csvfileoutput, array($product_id,$sku,$productname,$productname,$price,$description,$shortdescription,$url));
}

?>

请让我知道我做错了什么,任何建议都会有所帮助。

谢谢

【问题讨论】:

  • 查看$csvfileoutput = fopen($csv_fileoutput, 'a');fopen()Returns a file pointer resource on success, or FALSE on error.的回复
  • CSV_PATH 的值不以斜杠开头,因此您可能正在尝试写入不存在的目录。

标签: php file csv fputcsv


【解决方案1】:

如果文件根本没有被创建,这意味着这是fopen 的问题,而不是fputcsv

您需要在这里进行一些调试。首先使用var_dump 转储$csvfileoutput。可能是false,表示打开文件失败。

如果确实是false,那么我猜这是权限问题。检查 Web 服务器的用户(通常是 www-data,如果您使用的是 Apache)在您尝试创建文件的路径(CSV_PATH)中是否具有写入权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多