【发布时间】:2016-07-15 13:50:41
【问题描述】:
我已经制定了将数据从 .csv 文件上传到数据库所需的 PHP 代码,而不是使用 CakePHP3.x,但我正在努力将其集成到我的框架中。我不想将文件上传到数据库,只从 CSV 文件中读取数据。
这是我在本地主机上工作的代码,用于将数据上传到我的数据库。
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$fp = fopen("CSV_Upload_Test.csv", "r");
while( !feof($fp)) {
if( !$line = fgetcsv($fp, 1000, ',', '"')) {
continue;
}
$mysqlimport = "INSERT INTO divisiontwosix.csvtestsystems VALUES('".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."')";
mysql_query($mysqlimport) or die(mysql_error());
}
fclose($fp);
这是我在我的服务器上安装 CakePHP 的代码,但我似乎找不到(或者更可能问正确的问题以找到)如何实现它的答案。
这是我的控制器:
public function upload()
{
$system = $this->Systems->newEntity();
//Check if file has been uploaded.
if(!empty($this->request->data['Systems']['upload']['name']))
{
$file = $this->request->data['Systems']['upload'];
}
if ($this->request->is('post')) {
$system = $this->Systems->patchEntity($system, $this->request->data);
// add upload data to controller function upload
$file = $_REQUEST['text'];
$fp = fopen("$file","r");
while( !feof($fp)) {
if( !$line = fgetcsv($fp, 1000, ',', '"')) {
continue;
}
$mysqlimport = "INSERT INTO divtwosix.systems VALUES('".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."')";
mysql_query($mysqlimport) or die(mysql_error());
}
fclose($fp);
if ($this->Systems->save($system)) {
$this->Flash->success(__('The system has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The system could not be saved. Please, try again.'));
}
}
$estimates = $this->Systems->Estimates->find('list', ['limit' => 200]);
$this->set(compact('system', 'estimates'));
$this->set('_serialize', ['system']);
}
这是我的表单 (upload.ctp),如您所见,我尝试了几件事:
<?= $this->Form->create($system) ?>
<fieldset>
<legend><?= __('Upload System') ?></legend>
<?php
echo $this->Form->create('User', array('url' => array('action' => 'create'), 'enctype' => 'multipart/form-data'));
// echo $this->Form->create($system, ['type'=>'file']);
echo $this->Form->input('estimates_id', ['label'=>'Select Estimate Name','options' => $estimates]);
// echo $this->Form->input('file', ['label'=>'Upload works with CSV files only','type' => 'file']);
echo $this->Form->input('text', ['label'=>'Input File location']);
?>
</fieldset>
<?= $this->Form->button(__('Upload')) ?>
我得到的错误是
fopen(C:\Users\jrogers\Desktop\Book1.csv): failed to open stream: No such file or directory [APP/Controller/SystemsController.php, line 86]
或
fopen(Book1.csv): failed to open stream: No such file or directory [APP/Controller/SystemsController.php, line 86]
我无意将文件添加到数据库中,而只是将 .csv 文件中的数据添加到数据库中。我意识到我可能遗漏了一些简单的东西,但非常感谢您的帮助。我找到的示例仅显示如何将文件上传到数据库,而不是如何从本地文件中读取数据。在此先感谢您的帮助。
【问题讨论】:
-
为什么你使用文本输入作为文件名而不是你已经注释掉的文件输入?文本输入会让您告诉服务器文件的名称,但该文件在您的计算机上,而不是服务器上,因此它无法打开它也就不足为奇了。您需要使用文件输入,以便将文件发送到服务器,在该请求期间可以在 tmp 文件夹中读取该文件。
标签: php csv upload cakephp-3.0