我正在构建一个将多个图片上传到我们网站的功能,到目前为止,我已将此作为我的上传表单:
<?php
namespace Tyson\AdminBundle\Form;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
class IuType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class);
$builder->add('image', FileType::class, array(
'attr' => array(
'multiple' => 'true',
'data_class' => null
)
));
}
public function getName()
{
return 'iu';
}
}
然后将其显示在Twig文件中,如下所示:
<div class="tabs-panel" id="panel3v">
{{ form_start(iu,{action:path('admin_upload-images')}) }}
<div class="row">
<div class="small-3 medium-3 large-3 columns field-label">
<label>Image ID</label>
</div>
<div class="small-9 medium-9 large-9 columns">
{{ form_widget(iu.id) }}
{{ form_errors(iu.id) }}
</div>
</div>
<div class="row">
<div class="small-3 medium-3 large-3 columns field-label">
<label>Image Upload</label>
</div>
<div class="small-9 medium-9 large-9 columns">
{{ form_widget(iu.image) }}
{{ form_errors(iu.image) }}
</div>
</div>
<div class="row">
<div class="small-6 medium-6 large-6 columns">
<button type="submit" class="button" id="saveBtn">Save Changes</button>
</div>
</div>
{{ form_end(iu) }}
</div>
提交时,在测试期间,我输出表格传递给控制器的内容。这是输出:
[files] => Symfony\Component\HttpFoundation\FileBag Object
(
[parameters:protected] => Array
(
[iu] => Array
(
[image] => Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => CnY8dRqXYAAoaAw.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 35164
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => /tmp/phpibZuVW
[fileName:SplFileInfo:private] => phpibZuVW
)
)
)
我可以选择一个图像,或100个图像,但在控制器执行任何其他操作之前,表单仍然只传递了一个图像。
作为参考,这是我控制器的代码:
<?php
namespace Tyson\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\User\UserInterface;
use Tyson\CoreBundle\Entity\Images;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class UploadController extends Controller
{
public function uploadimagesAction(Request $request)
{
$fs = new Filesystem();
$dm = $this->getDoctrine()->getManager();
$dir = '/var/www/html/tyson/web/uploads/images/';
$request = $this->get('request_stack')->getCurrentRequest();
$formdet = $request->request->get('iu');
$imgId = $formdet['id'];
$folderPath = '/var/www/html/tyson/web/uploads/images/'.$imgId;
$imgDirectory = '/var/www/html/tyson/web/uploads/images/'.$imgId.'/';
$folderCheck = $fs->exists($folderPath);
print_r($request);
die();
}
}
我想象file
输出应该包含多个图像到控制器(因为表单提交一次,而不是重复),所以我做错了什么呢?< / p>
答案 0 :(得分:0)
同样的问题来自我
$builder->add('files', FileType::class, array('required' => false,
'mapped' => false,
'attr' => array(
'multiple' => true
),
));
只提交了一个文件。暂时没有我的解决方案,我将使用javascript添加[]到上传名称。
答案 1 :(得分:0)
我在Symfony 4.2上遇到了这个问题,在fileBag的数组中只有1张图像,我在添加了multiple属性后才运行php bin/console cache:clear
答案 2 :(得分:0)
您是否尝试过使用“多个”选项(不是“ attr”选项中的那个)?
$builder
->add('files', FileType::class, [
'multiple' => true,
'attr' => [
'multiple' => 'multiple'
]
]
;