我正在使用此捆绑包通过表单上传文件。这就是我所做的。在我使用捆绑包的 Twig 模板中
<form action="{{ path('guardar-natural') }}" method="POST" class="form-horizontal" role="form" id="registroNatural" {{ form_enctype(form) }}>
{{ form_widget(form.documento_rif) }}
<button type="submit" class="btn btn-primary" id="btnEnviarRegistro">{{ 'registration.submit'|trans }}</button>
</form>
在表单类中:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('documento_rif', 'vich_file', array(
'required' => true,
'mapping' => 'recaudos',
'allow_delete' => true,
'download_link' => true,
'label' => false,
))
->add('usuario', new RegistrationForm());
}
在字段所在的实体中:
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @Vich\Uploadable
*/
class Natural
{
/**
* @Vich\UploadableField(mapping="recaudos", fileNameProperty="documentoRIF")
* @var File $imageFile
*/
protected $imageFile;
/**
* @ORM\Column(type="string", length=255, name="documento_rif", nullable=true)
* @var string $documentoRIF
*/
protected $documentoRIF;
/**
* @ORM\Column(type="datetime", nullable=true)
* @var \DateTime $updatedAt
*/
protected $updatedAt;
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
$this->updatedAt = new \DateTime('now');
}
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $documentoRIF
*/
public function setDocumentoRIF($documentoRIF)
{
$this->documentoRIF = $documentoRIF;
}
/**
* @return string
*/
public function getDocumentoRIF()
{
return $this->documentoRIF;
}
}
在config.yml
文件:
vich_uploader:
db_driver: orm # or mongodb or propel or phpcr
storage: vich_uploader.storage.file_system
mappings:
recaudos:
uri_prefix: /recaudos
upload_destination: %kernel.root_dir%/../web/uploads/recaudos
namer: vich_uploader.namer_uniqid
delete_on_update: true
delete_on_remove: true
捆绑包已启用,因为我没有收到任何错误,目录具有正确的权限(0755)。在数据库级别,当在documento_rif
列中创建任何记录时,我得到这个字符串/tmp/php1fbjiZ
而不是文件名,我的代码有什么问题?
当然,我有更多关于这一切的代码,但只是在这里写下相关部分。