我是cakephp的新手,并尝试学习。
我的表结构是
表名 - 驱动程序
字段 - id,name,
表格 - drivers_uploads
字段 - id,driver_id,documentname,documentpath
我尝试在我的表格中上传多个文件。
我的代码在上传多个图片的文件夹中运行良好但在表格中我想插入所有行
我想为同一个id插入多个文件,id在驱动程序表中,其余文件在drivers_upload表中为同一个id插入多行。
提前感谢。
for ex
我需要以下表格式插入数据。
**司机表**
id,name
16幸运
driver_uploads表
id,driver_id,document_name
1 16 mo.docx
2 16 ro.docx
控制器代码
public function add() {
$this->Driver->create();
if ($this->request->is('post')) {
for($i=1;$i<4;$i++)
{
if(empty($this->data['Driver']['document'.$i]['name'])){
unset($this->request->data['Driver']['document'.$i]);
}
if(!empty($this->data['Driver']['document'.$i]['name']))
{
$file=$this->data['Driver']['document'.$i];
$ary_ext=array('jpg','jpeg','gif','png','xlsx','docx'); //array of allowed extensions
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
if(in_array($ext, $ary_ext))
{
move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. time().$file['name']);
//move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/posts/' . mktime().$file['name']);
$this->request->data['Driver']['document'.$i] = time().$file['name'];
}
}
}
if ($this->Driver->save($this->request->data))
{
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
else
{
$this->Session->setFlash('Unable to add your post.');
}
}
}
查看代码
<h1>Add Post</h1><?php
echo $this->Form->create('Post', array('url' => array('action' => 'add'), 'enctype' => 'multipart/form-data'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
for($i=1; $i<4; $i++)
{
?>
<div id="attachment<?php echo $i;?>" <?php if($i !=1) echo "style='display:none;'";?> >
<div>
<?php echo $this->Form->input('image'.$i,array('type'=>'file','label' => false,'div' => false));?>
</div>
<div id="attachmentlink<?php echo $i;?>" <?php if($i==3) echo "style='display:none;'";?>><a href="javascript:void(0);" onclick="show('attachment<?php echo $i+1;?>'); hide('attachmentlink<?php echo $i;?>');">Add Another Attachment</a></div>
</div>
<?php } ?>
<?php
echo $this->Form->end('Save Post');
?>
<script>
function show(target){
document.getElementById(target).style.display = 'block';
}
function hide(target){
document.getElementById(target).style.display = 'none';
}
</script>
以下是我的print_r数据
echo "<pre>";print_r($this->request->data); exit();
if ($this->Driver->save($this->request->data))
Array
(
[Driver] => Array
(
[name] => raj
[mobile] => 9960181380
[email] => raj@gmail.com
[licenceno] => 512203615803
[address] => 452,nagpur
[document1] => Array
(
[name] => findmelogistics_Phase_III_Project Plan v1.2_8.xls
[type] => application/vnd.ms-excel
[tmp_name] => /tmp/phpXGkLvj
[error] => 0
[size] => 29184
)
[document2] => 1461815092FML Phase 3 Specification 2.docx
[document3] => 1461815092Phase 3 - SmartData questions 310316.docx
)
)