Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2,文件上载-在编辑中删除旧文件并创建新文件_Php_Symfony_File Upload - Fatal编程技术网

Php Symfony2,文件上载-在编辑中删除旧文件并创建新文件

Php Symfony2,文件上载-在编辑中删除旧文件并创建新文件,php,symfony,file-upload,Php,Symfony,File Upload,我有包括Image在内的working entity References.php,但我不知道如何在Symfony2中删除保存在此引用中的旧图像(如果存在)并创建新图像。因为现在,它没有删除当前图像,所以只创建了一个新的并将新的image\u路径设置到该实体中。这是我尝试在预上传方法中删除它,但它将当前文件设置为空,然后什么也没有(因此我有错误-您必须选择一个文件) 在PrePersist/PreUpdate方法之前测试image\u path属性上的@Assert\NotNull,因此表单验证

我有包括Image在内的working entity References.php,但我不知道如何在Symfony2中删除保存在此引用中的旧图像(如果存在)并创建新图像。因为现在,它没有删除当前图像,所以只创建了一个新的并将新的
image\u路径设置到该实体中。这是我尝试在
预上传
方法中删除它,但它将当前文件设置为
,然后什么也没有(因此我有错误-您必须选择一个文件


在PrePersist/PreUpdate方法之前测试image\u path属性上的@Assert\NotNull,因此表单验证不满意,因为image\u path仅在实体内部提供,请求不向表单提供“image\u path”属性,我认为您应该删除这个断言,因为它没有链接到表单,所以我认为它不是真正有用的

您的旧图像路径是新的,而不是旧的,因为它是在表单绑定之后处理的。

您应该使用,这比实体中的注释事件要好得多,以便您能够在preUpdate事件中检索正确的值

您可以使用以下方法:

hasChangedField($fieldName) to check if the given field name of the current entity changed.
getOldValue($fieldName) and getNewValue($fieldName) to access the values of a field.
setNewValue($fieldName, $value) to change the value of a field to be updated.

如果已设置图像路径,则存在要替换的“旧”图像

在您的
upload()
方法中,而不是

    // set the path property to the filename where you've saved the file
    $this->image_path = $this->file->getClientOriginalName();
。。。检查是否存在以前的文件,并在执行以下操作之前将其删除:

 if ($this->image_path) {
     if ($file = $this->getAbsolutePath()) {
        unlink($file);
    }
 }
 $this->image_path = $this->file->getClientOriginalName();

所以我找到了解决办法。首先,我必须为文件上传创建一个断言回调,因为我使用的是
NotNull()
Assert For
Reference
entity。所以,如果我选择了任何文件并发送了表单,我总是会遇到错误
您必须选择一个文件
。所以我的第一次编辑就在这里:

use Symfony\Component\Validator\ExecutionContextInterface;      // <-- here

/**
 * @ORM\Entity(repositoryClass="Acme\ReferenceBundle\Entity\ReferenceRepository")
 * @ORM\Table(name="`references`") 
 * @ORM\HasLifecycleCallbacks 
 * @Assert\Callback(methods={"isFileUploadedOrExists"})       <--- and here
 */
class Reference
{
    // code
}
此外,我还删除了我的
$image\u path
属性中的
NotNull
断言

然后它成功地工作了——如果我选择了一个文件并提交了表单,引用是用图像创建的。但它还没有完成。我在这个问题中提出了一个问题-删除旧图像,然后用新路径创建一个新图像

经过多次实验,我找到了一个既有效又美观的解决方案。在我的控制器中,我在表单验证之前和用于删除旧图像之后添加了一个变量:

$oldImagePath = $reference->getImagePath(); // get path of old image

if($form->isValid())
{
    if ($form->get('file')->getData() !== null) { // if any file was updated
        $file = $form->get('file')->getData();
        $reference->removeFile($oldImagePath); // remove old file, see this at the bottom
        $reference->setImagePath($file->getClientOriginalName()); // set Image Path because preUpload and upload method will not be called if any doctrine entity will not be changed. It tooks me long time to learn it too.
    }
    $em->persist($reference);
    try {
        $em->flush();
    } catch (\PDOException $e) {
        //sth
    }
和我的
removeFile()
方法:

public function removeFile($file)
{
    $file_path = $this->getUploadRootDir().'/'.$file;
    if(file_exists($file_path)) unlink($file_path);
}
最后,我删除了
$this->image\u path=$this->file->getClientOriginalName()upload()
方法中的code>line,因为它会导致表单中的预览图像出现问题(如果使用)。它将原始文件名设置为路径,但如果重新加载页面,您将看到图像的真实路径。删除此行将解决此问题


感谢大家发布答案,谁帮我找到了解决方案。

这很有用,因为如果没有它,如果有人试图发布新表单而没有上传图像,他将得到一个MySQL异常,这不是很漂亮。那么,我如何获得旧的图像路径呢?我会在调用form->HandlerRequest或form->bind之前尝试获取它,在实体中使用不同的方法,因此在绑定后,实体是干净的,但是,如果表单未成功验证,我不想删除旧图像在验证之前不要删除图像,只需将其存储在case中的某个位置,并且仅当表单有效时调用Entity->deleteOldImageIfExists(),以创建新的引用消息(您必须选择一个文件)如果我选择要上载的图像。
$oldImagePath = $reference->getImagePath(); // get path of old image

if($form->isValid())
{
    if ($form->get('file')->getData() !== null) { // if any file was updated
        $file = $form->get('file')->getData();
        $reference->removeFile($oldImagePath); // remove old file, see this at the bottom
        $reference->setImagePath($file->getClientOriginalName()); // set Image Path because preUpload and upload method will not be called if any doctrine entity will not be changed. It tooks me long time to learn it too.
    }
    $em->persist($reference);
    try {
        $em->flush();
    } catch (\PDOException $e) {
        //sth
    }
public function removeFile($file)
{
    $file_path = $this->getUploadRootDir().'/'.$file;
    if(file_exists($file_path)) unlink($file_path);
}