我有一个控制器有一个动作添加,它基本上上传了服务器上的图像
function add($ 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')
?>
我希望能够让用户选择能够选择多个文件。 例如,如果我转到cakephp / images / add / 3,它将创建3个上传选择表单。
在add.ctp中实现此选项的最佳方法是什么
答案 0 :(得分:0)
这是一个简单的示例,说明如何执行此操作。此表单不包含任何模型数据,但很容易更改。 $this->data['fileinfo']
将是一个包含n
元素的数组。
<强>控制器:强>
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');
?>