アプリに写真をアップロードしたい。
ProductController.php
// ...
public function addAction()
{
$categories = $this->getCategoryTable()->fetchAll();
$category_options = array();
foreach ($categories as $category) {
$category_options[$category->id] = $category->name;
}
$form = new ProductForm($category_options);
$form->get('submit')->setValue('Pridať inzerát');
$request = $this->getRequest();
if ($request->isPost()) {
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($post);
$product = new Product();
$form->setInputFilter($product->getInputFilter());
if ($form->isValid())
{
$data = $form->getData();
echo var_dump($data);
$product->exchangeArray($form->getData());
$this->getProductTable()->saveProduct($product);
//return $this->redirect()->toRoute('product');
}
}
return array(
'form' => $form,
);
}
ProductForm.php
class ProductForm extends Form
{
public function __construct($category_options)
{
parent::__construct('product');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'title',
'type' => 'text',
'options'=> array(
'label' => 'Názov inzerátu:',
),
));
$this->add(array(
'name' => 'description',
'type' => 'text',
'options' => array(
'label' => 'Popis inzererátu:',
),
));
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Kategória inzerátu:',
'value_options' => $category_options
),
));
$this->add(array(
'name' => 'phone',
'type' => 'text',
'options' => array(
'label' => 'Telefónne číslo:',
),
));
$this->add(array(
'name' => 'email',
'type' => 'text',
'options' => array(
'label' => 'Email:',
),
));
$this->add(array(
'name' => 'price',
'type' => 'text',
'options' => array(
'label' => 'Cena:',
),
));
$this->add(array(
'name' => 'location',
'type' => 'text',
'options' => array(
'label' => 'Mesto:',
),
));
$this->add(array(
'name' => 'shipping',
'type' => 'text',
'options' => array(
'label' => 'Odosiela do:',
),
));
$this->add(array(
'name' => 'top',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' =>'Topovať inzerát?',
'value_options' => array(
'nie' => 'Nie',
'ano' => 'Áno'
),
),
));
$this->addPhotos();
$this->addInputFilter();
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Choď',
'id' => 'submitbutton',
'class' => 'btn btn-primary'
),
));
}
public function addPhotos()
{
// File Input
$file = new Element\File('image-file');
$file->setLabel('Foto pre inzerát')
->setAttribute('id', 'image-file')
->setAttribute('name', 'image-file')
->setAttribute('multiple', true);
$this->add($file);
}
public function addInputFilter()
{
$inputFilter = new InputFilter\InputFilter();
// File Input
$fileInput = new InputFilter\FileInput('image-file');
$fileInput->setRequired(true);
$fileInput->getValidatorChain()
->attachByName('filesize', array('max' => 204800))
->attachByName('filemimetype', array('mimeType' => 'image/jpeg, image/x-png'))
->attachByName('fileimagesize', array('maxWidth' => 100, 'maxHeight' => 100));
$fileInput->getFilterChain()->attachByName(
'filerenameupload',
array(
'target' => './public/img/product.jpeg',
'randomize' => true,
)
);
$inputFilter->add($fileInput);
$this->setInputFilter($inputFilter);
}}
そのマルチアップロードフォームですが、写真フォームをアップロードしたい場合は、「製品」を挿入し、写真を挿入しないでください。var_dump() を使用して配列をエコーし、パスに変更はありません。次に、データベースへのファイルパスを書きたいと思います。すべてのヘルプは大歓迎です。ありがとう。