【发布时间】:2016-06-27 05:56:13
【问题描述】:
我正在使用 ZF2 多张图片上传,这意味着我将文件按钮放在一个下方,并尝试一个接一个地上传,但它给出了以下错误。请帮助我找出错误,如果有相同的代码可以正常工作表单中只有一个文件标签。
注意:第 989 行 C:\xampp\htdocs\ZendFirstProjectNew1\module\Stall\src\Stall\Controller\StallController.php 中的数组到字符串转换
我被困在这里,代码的详细信息如下
表格
namespace Stall\Form;
use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Adapter;
use Doctrine\ORM\EntityManager;
class MultipleImageForm extends Form
{
protected $em;
public function __construct($name = null)
{
parent::__construct('stall');
$this->setAttribute("method","post");
$this->setAttribute("enctype","multipart/form-data");
$this->add(array(
'name' => 'file',
'attributes' => array(
'type' => 'file',
),
'options' => array(
'label' => 'First Image',
),
));
$this->add(array(
'name' => 'image',
'attributes' => array(
'type' => 'file',
),
'options' => array(
'label' => 'Second Image',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'submit',
));
}
}
HTML
$form->setAttribute('action',$this->url('stall',array('action'=>'multipleimage')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('file'));
echo '<br/>';
echo $this->formRow($form->get('image'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
控制器
$form = new MultipleImageForm();
$form->get('submit')->setValue('Submit');
$request = $this->getRequest();
if($request->isPost())
{
$nonFile = $request->getPost()->toArray();
$File = $this->params()->fromFiles('file');
$data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
// print_r($data);die;
//set data post and file ...
$form->setData($data);
if ($form->isValid())
{
$result = new ImageUpload();
$image1 = (string) $data['file']['name'];
$image2 = (string) $data['image']['name'];
if(!empty($image1))
{
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination('public/img/upload/'); // Returns all known internal file information
$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR .$image1 , 'overwrite' => true));
//$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR."_upload123".$favicon , 'overwrite' => true));
if(!$adapter->receive())
{
$messages = $adapter->getMessages();
}
else
{
echo "Image Uploaded";
}
}
if(!empty($image2))
{
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination('public/img/upload/'); // Returns all known internal file information
$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR .$image2 , 'overwrite' => true));
//$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR."_upload123".$favicon , 'overwrite' => true));
if(!$adapter->receive())
{
$messages = $adapter->getMessages();
}
else
{
echo "Image Uploaded";
}
}
}
}
return array('form' => $form);
【问题讨论】:
-
控制器中的哪一行是
989行? -
$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR .$image1 , 'overwrite' => true));这是第 989 行
标签: php image forms zend-framework2