-3
我想從csv文件導入到數據庫的數據,但我不能這樣做。我有下面的代碼。我想從csv打印數據呢,但它給了我錯誤:symfony文件導入(csv)
Warning: fopen(soubor.csv): failed to open stream: No such file or directory in /var/www/VWM/src/VWM/ProjektBundle/Controller/DefaultController.php line 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,
);
}
您已回答了您自己的問題。該文件不存在(您需要更正路徑)。警告說明了這一點。 – 2013-05-01 15:27:03
那麼,我的代碼有什麼問題? :-( – user2129659 2013-05-01 15:39:39
您正在打開一個文件** **無路徑 – 2013-05-01 22:16:31