我有一个控制器,它有一个动作添加,基本上是在服务器上上传图像
函数相加($number_of_images=1){
$this->set('number_of_images', $number_of_images); if (!empty($this->data)) { $count = 1; foreach($this->data['Images'] as $entry){ $file_name = "file" . $count; if ($data_s = $this->Uploader->upload($file_name)) { $this->Image->saveAll($data_s); } $count++; } $this->Session->setFlash("Your image(s) has been saved"); $this->redirect(array('action'=>'index')); } }
我的添加视图基本上允许用户选择1个文件并上传
<?php
echo $this->Form->create('Images', array('type' => 'file', 'url' => array('controller' => 'images', 'action' => 'add')));
$count = 1;
while($count <= $number_of_images){
// file name is file + image number
$file_name = 'file' . $count;
echo $this->Form->input($file_name, array('type' => 'file'));
// echo $this->Form->input("File " . $count, array('type' => 'file'));
$count++;
}
echo $this->Form->end('Upload');
echo $this->Html->link('Go Back To Main Page', '/images')
?>
我希望能够让用户有一个选项,能够选择一个以上的文件。例如,如果我转到cakehp/images/add/3,它将创建3个上传选择表单。
在add.ctp 中实现此选项的最佳方式是什么?
这里有一个简单的例子,说明了如何做到这一点。此表单不包括任何模型数据,但更改起来很容易。CCD_ 1将是具有CCD_ 2元素的阵列。
控制器:
function add() {
if(!empty($this->data)) {
foreach($this->data['fileinfo'] as $f) {
switch($f['error']) {
case 4:
// nothing to do (i.e. user did not select a file)
break;
case 0:
// handle uploaded file
break;
default:
// report error message
}
}
}
$this->set('n', 5); // number of files that can be uploaded
}
视图:
<?
echo $this->Form->create(false, array('enctype' => 'multipart/form-data'));
for($i = 0; $i < $n; $i++) {
echo $this->Form->file("fileinfo.$i");
}
echo $this->Form->end('Upload');
?>