移动上传的文件另一个目录

时间:2016-11-02 13:52:28

标签: symfony

我正在删除文件。现在我想移动软删除文件'trashedFile'目录。我的软删除运行,但我无法移动到目标文件夹。 我的UploadedFile实体如下。我的文件存储在../uploads/file中。如果删除文件,请移至../ uploads / file / trashedFiles。

我的代码:

 $deleteFile = $this->em->
 getRepository('AppBundle:UploadedFile\UploadedFile')
->findOneBy(array('id'=>$id));
 $this->em->remove($deleteFile);

$move = $deleteFile->
rename('%kernel.root_dir%/../uploads/files/trashedFiles',$name); //This section is my problem??? 

UploadedFile实体:

/**
* UploadedFile
* @Gedmo\SoftDeleteable
* @ORM\Table(name="uploaded_files")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UploadedFile\UploadedFileRepository")
* @Vich\Uploadable
*/
class UploadedFile
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @Vich\UploadableField(mapping="file", fileNameProperty="uniqueName")
 * @var File
 */
private $documentFile;

/**
 * @var string
 *
 * @ORM\Column(name="orginal_name", type="string", length=255)
 */
private $orginalName;

/**
 * @var int
 *
 * @ORM\Column(name="size", type="integer")
 */
private $size;

/**
 * @var string
 *
 * @ORM\Column(name="type", type="string", length=20)
 */
private $type;

/**
 * @var string
 *
 * @ORM\Column(name="unique_name", type="string", length=255, unique=true)
 */
private $uniqueName;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=255)
 */
private $Url;

/**
 * @var User
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
 */
private $user;

/**
 * @var boolean
 *
 * @ORM\Column(type="boolean")
 */
private $active;

use TimestampableEntity;

use SoftDeleteableEntity;

/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set orginalName
 *
 * @param string $orginalName
 *
 * @return UploadedFile
 */
public function setOrginalName($orginalName)
{
    $this->orginalName = $orginalName;

    return $this;
}

/**
 * Get orginalName
 *
 * @return string
 */
public function getOrginalName()
{
    return $this->orginalName;
}

/**
 * Set size
 *
 * @param integer $size
 *
 * @return UploadedFile
 */
public function setSize($size)
{
    $this->size = $size;

    return $this;
}

/**
 * Get size
 *
 * @return int
 */
public function getSize()
{
    return $this->size;
}

/**
 * Set type
 *
 * @param string $type
 *
 * @return UploadedFile
 */
public function setType($type)
{
    $this->type = $type;

    return $this;
}

/**
 * Get type
 *
 * @return string
 */
public function getType()
{
    return $this->type;
}

/**
 * Set uniqueName
 *
 * @param string $uniqueName
 *
 * @return UploadedFile
 */
public function setUniqueName($uniqueName)
{
    $this->uniqueName = $uniqueName;

    return $this;
}

/**
 * Get uniqueName
 *
 * @return string
 */
public function getUniqueName()
{
    return $this->uniqueName;
}

/**
 * @return string
 */
public function getUrl()
{
    return $this->Url;
}

/**
 * @param string $Url
 */
public function setUrl($Url)
{
    $this->Url = $Url;
}

/**
 * @return User
 */
public function getUser()
{
    return $this->user;
}

/**
 * @param User $user
 */
public function setUser($user)
{
    $this->user = $user;
}

/**
 * @return boolean
 */
public function isActive()
{
    return $this->active;
}

/**
 * @param boolean $active
 */
public function setActive($active)
{
    $this->active = $active;
}

/**
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
 *
 * @return $this
 */
public function setDocumentFile(File $file = null)
{
    $this->documentFile = $file;

    if ($file) {
        $this->updatedAt = new \DateTime('now');
    }

    return $this;
}

/**
 * @return File|null
 */
public function getDocumentFile()
{
    return $this->documentFile;
}

}

1 个答案:

答案 0 :(得分:1)

从实体管理器中删除实例后,您将无权访问它

$this->em->remove($deleteFile); // $deleteFile = null

软删除的概念实际上是一种不删除实体的方法,但是为它添加另一个布尔字段,表示文件是否被“删除”。

如果您仍想保持逻辑,请先将文件移动到另一个文件夹,然后从db中删除该实体。

希望这有帮助!