【问题标题】:Not able to get the filenames of uploaded files in symfony2无法在 symfony2 中获取上传文件的文件名
【发布时间】:2013-05-21 22:09:30
【问题描述】:

我已经关注了使用 Symfony2 上传多个文件并创建了一个实体

/*
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="Repair\StoreBundle\Entity\attachmentsRepository")
 */

class attachments
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */

    private $id;
    /**
     * @var string
     * 
     * @Assert\File(maxSize="6000000")
     * @ORM\Column(name="files", type="array", length=255, nullable=true)
     */
    private $files=array();

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

    /**
     * Set files
     * @param object $files
     * 
     * @return attachments
     */
    public function setFiles($files) {
        $this->files = $files;
    }

    /**
     * Get files
     *
     * @return object 
     */
    public function getFiles() {
        return $this->files;
    }

    public function __construct() {
        $files = array();
    }

     public function uploadFiles() {
        // the files property can be empty if the field is not required
        if (null === $this->files) {
            return;
        } 

        if (!$this->id) {
                $this->files->move($this->getTmpUploadRootDir(), $this->files->getClientOriginalName());

        } else {
            $this->files->move($this->getUploadRootDir(), $this->files->getClientOriginalName());
        }

        $this->setFiles($this->files->getClientOriginalName());
    }

    public function getAbsolutePath() {
        return null === $this->path
            ? null
            : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path;
    }

    public function getWebPath() {
        return null === $this->path
            ? null
            : $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path;
    }

    protected function getUploadRootDir() {
        return __DIR__ . '/../../../../web/'. $this->getUploadDir();
    }

    protected function getUploadDir() {
        return 'uploads/';
    }

}

我有一个控制器,它有以下代码

class uploadController extends Controller
{

    public function uploadAction(Request $request) {

        $id= $_GET['id'];

        $user = new attachments();

        $form = $this->createFormBuilder($user)->add('files','file',array("attr"=>array("multiple" =>"multiple",)))->getForm();

        $formView = $form->createView();

        $formView->getChild('files')->set('full_name','form[file][]');

        if ($request->getMethod() == 'POST') {

            $em = $this->getDoctrine()->getManager();
            $form->bind($request);
            $files = $form["files"]->getFilenames();
            $user->uploadFiles(); //--here iam not able to get te file names in order to send to the upload function.upload files is returning null values

       }
   }
}

控制器无法从视图中获取 uder 上传的文件名。当我发送到实体中的上传函数时,它返回空值

【问题讨论】:

  • 不清楚您的意思,能否提供除代码之外的更多信息?
  • 我想上传多个文件。为此,当我尝试使用表单从控制器检索上传的文件时,我需要获取用户从视图上传到控制器的文件名[ "files"]->getData();它返回空值
  • 当我通过 $em = $this->getDoctrine()->getManager();在上传函数中,它返回空值,即它无法将文件名从控制器获取到实体以便上传。在控制器本身中,它无法从视图中获取文件名

标签: php symfony upload


【解决方案1】:

我认为您应该再次阅读Documentation about File Upload。您正确使用注释 @ORM\HasLifecycleCallbacks 但您的代码缺少方法的正确注释。在验证请求并持久化对象后,您可以使用@ORM\PostPersist()@ORM\PostUpdate() 注释自动调用upload() 函数。另外我建议为每个文件使用一个实体并创建一个OneToMany 关系。这将使正确存储文件变得更加容易和合乎逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多