【问题标题】:symfony file import (csv)symfony 文件导入 (csv)
【发布时间】:2013-05-01 14:48:25
【问题描述】:

我想将数据从 csv 文件导入数据库,但我做不到。我有下面的代码。我还想从 csv 打印数据,但它给了我错误:

警告:fopen(soubor.csv):无法打开流:/var/www/VWM/src/VWM/ProjektBundle/Controller/DefaultController.php 第 56 行中没有这样的文件或目录

我该如何解决?

public function addProductAction() {
    $product = new Product;
    $form = $this->createForm(new ProductType(), $product);
    $form2 = $this->get('form.factory')->createNamedBuilder('form2name')
        ->add('submitFile', 'file', array('label' => 'CSV'))
        ->getForm();

    $m = 'nevyplněno';
    $request = $this->getRequest();
    if('POST' === $request->getMethod()) {

        // add one product, it works
        if($request->request->has('vwm_projektbundle_producttype')) {
            $form->bindRequest($request);
            if($form->isValid()) {
                return $this->forward('VWMProjektBundle:Default:result', array('product' => $product));
            } else {
                $m = 'Chybně vyplněný formulář';
            }
        }

        // add products from CSV
        if($request->request->has('form2name')) {
            $form2->bindRequest($request);
            if($form2->isValid()) {
                // Get File
                $file = $form2->get('submitFile');
                // your csv file here when you hit submit button
                $filename = $file->getData();           
                //print_r($data); // this returns filename.csv
                $row = 1;
                if (($handle = fopen($filename, "r")) !== FALSE) {
                    while (($data = fgetcsv($handle, 100, ";")) !== FALSE) {
                        $num = count($data);
                        echo "<p> $num fields in line $row: <br /></p>\n";
                        $row++;
                        for ($c=0; $c < $num; $c++) {
                            echo $data[$c] . "<br />\n";
                      }
                    }
                    fclose($handle);
                }
            }
        }
    }

    return array(
        'form' => $form->createView(),
        'form2' => $form2->createView(),
        'm' => $m,
        );
}

【问题讨论】:

  • 您已经回答了自己的问题。该文件不存在(您需要更正路径)。警告说明了这一点。
  • 那么我的代码有什么问题? :-(
  • 您正在打开一个文件没有路径
  • 错误信息告诉你你需要知道的一切。有“没有这样的文件或目录”查看/var/www/VWM/src/VWM/ProjektBundle/Controller/DefaultController.php中的第56行

标签: php symfony


【解决方案1】:

替换这些行:

$filename = $file->getData();           
//print_r($data); // this returns filename.csv

与:

$filename = $file->getData();           
print_r($data); // this returns filename.csv
die(' <-- actual path');

您看到的文件路径是否正确?

要测试您的代码,您可以简单地将绝对路径放在 $filename 变量中。如果它不适用于正确的绝对路径,请检查文件的权限。

【讨论】:

    【解决方案2】:

    尝试使用:

    $csvFile = $form->get('csvFile')->getData()->getPathName();
    

    它将返回您的完整临时文件路径名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2018-12-01
      • 2013-02-07
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多