gpt4 book ai didi

php - 文件未上传,临时名称已插入数据库,我做错了什么?

转载 作者:行者123 更新时间:2023-12-02 07:27:15 25 4
gpt4 key购买 nike

我正在使用这个包通过表单上传文件。这就是我所做的。在我使用 bundle 的 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 而不是文件名,我的代码有什么问题?

当然,我有更多关于所有这些的代码,但只在这里写下相关部分。

最佳答案

在您的表单类型中,您应该引用可上传字段 (imageFile) 而不是文件名列 (documento_rif)。

与您的错误无关,但会导致问题:映射中定义的 uri_prefix 不正确,它应该是 /uploads/recaudos

其他的应该没问题。

关于php - 文件未上传,临时名称已插入数据库,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com