将Dropzone与Symfony2集成,提供文件""不存在

时间:2013-12-05 11:40:47

标签: php exception symfony dropzone.js

我正在尝试将Dropzone与symfony集成。但是,当我选择一个文件时,我会遇到以下异常:

 The file "" does not exist
 500 Internal Server Error - FileNotFoundException
 Stack Trace    
    in /var/www/thegabrielhotel/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php at line 115  -
            public function guess($path)
            {
                if (!is_file($path)) {
                    throw new FileNotFoundException($path);
                }
                if (!is_readable($path)) {
    at MimeTypeGuesser ->guess ('')
    in /var/www/thegabrielhotel/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/File.php at line 87  +
    at File ->getMimeType ()
    in /var/www/thegabrielhotel/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/File.php at line 64  +
    at File ->guessExtension ()
    in /var/www/thegabrielhotel/src/Gbr/BEBundle/Document/OverviewPhoto.php at line 71  +
    at OverviewPhoto ->preUpload ()
    in /var/www/thegabrielhotel/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php at line 525  +
    at ClassMetadataInfo ->invokeLifecycleCallbacks ('prePersist', object(OverviewPhoto))
    in /var/www/thegabrielhotel/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php at line 953  +
    at UnitOfWork ->persistNew (object(ClassMetadata), object(OverviewPhoto))
    in /var/www/thegabrielhotel/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php at line 1775  +
    at UnitOfWork ->doPersist (object(OverviewPhoto), array('000000001f48b19e00007fd7abcd7e2a' => object(OverviewPhoto)))
    in /var/www/thegabrielhotel/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php at line 1739  +
    at UnitOfWork ->persist (object(OverviewPhoto))
    in /var/www/thegabrielhotel/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentManager.php at line 384  +
    at DocumentManager ->persist (object(OverviewPhoto))
    in /var/www/thegabrielhotel/src/Gbr/BEBundle/Controller/DefaultController.php at line 57  +
    at DefaultController ->postOverviewPhotosAction (object(Request))
    at call_user_func_array (array(object(DefaultController), 'postOverviewPhotosAction'), array(object(Request)))
    in kernel.root_dir/bootstrap.php.cache at line 2843  +
    at HttpKernel ->handleRaw (object(Request), '1')
    in kernel.root_dir/bootstrap.php.cache at line 2817  +
    at HttpKernel ->handle (object(Request), '1', true)
    in kernel.root_dir/bootstrap.php.cache at line 2946  +
    at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
    in kernel.root_dir/bootstrap.php.cache at line 2248  +
    at Kernel ->handle (object(Request))
    in /var/www/thegabrielhotel/web/app_dev.php at line 28  +

这是dropzone的布局部分:

<form action="{{ path("gbr_be_post_overview_photos") }}" class="dropzone" id="my-dropzone">
                        </form>

这是我的postOverviewPhotosAction后期行动:

public function postOverviewPhotosAction(Request $request)
    {
        $dm = $this->get("doctrine_mongodb")->getManager();
        $files = $request->files;
        foreach($files as $file)
        {
            $overview_photo = new OverviewPhoto();
            $overview_photo->setPhotoFile($file);
            $dm->persist($overview_photo);
        }

        $dm->flush();
        return $this->render($this->generateUrl("gbr_be_get_overview"));
    }

OverviewPhoto文件:

<?php
namespace Gbr\BEBundle\Document;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(collection="overview_photos", repositoryClass="Gbr\BEBundle\Repository\OverviewPhotoRepository")
 */
class OverviewPhoto
{
    /**
     * @MongoDB\Id(strategy="AUTO")
     */
    protected $id;

    /**
     * @MongoDB\Date
     */
    protected $updated_at;

    /**
     * @Assert\Image
     */
    protected $photo_file;

    /**
     * @MongoDB\String
     */
    protected $photo_name;


    public function getUploadDir()
    {
        return 'uploads/images/overview/slider';
    }

    public function getRootUploadDir()
    {
        return "/../../../../web/". $this->getUploadDir();
    }

    public function getWebPath()
    {
        return $this->photo_name === null ? null : "/".$this->getUploadDir()."/".$this->photo_name;
    }

    public function getAbsolutePath()
    {
        return $this->photo_name === null ? null : "/".$this->getRootUploadDir()."/".$this->photo_name;
    }

    /**
     * WARNING!! PreUpdate no fired since $file is not managed by Doctrine.
     * SOLUTION: use PostLoad() Event.
     * @MongoDB\PostLoad()
     */
    public function postLoad()
    {
        $this->setUpdatedAt(new \DateTime());
    }

    /**
     * @MongoDB\PrePersist()
     * @MongoDB\PreUpdate()
     */
    public function preUpload()
    {
        if($this->photo_file !== null)
        {
            $this->photo_name = uniqid().".".$this->photo_file->guessExtension();
        }
    }

    /**
     * @MongoDB\PostPersist()
     * @MongoDB\PostUpdate()
     */
    public function upload()
    {
        if($this->photo_file === null)
        {
            return;
        }

        if(!file_exists($this->getUploadDir()))
        {
            mkdir($this->getUploadDir(),0777, true);
        }

        $this->photo_file->move($this->getUploadDir(), $this->photo_name);
        unset($this->photo_file);
    }

    /**
     * @MongoDB\PostRemove()
     */
    public function removeUpload()
    {
        if($this->getAbsolutePath() !== null){
            if(file_exists($this->getAbsolutePath()))
            {
                unlink($this->getAbsolutePath());
            }
        }
    }
    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set updatedAt
     *
     * @param date $updatedAt
     * @return self
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updated_at = $updatedAt;
        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return date $updatedAt
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

    /**
     * Set photoName
     *
     * @param string $photoName
     * @return self
     */
    public function setPhotoName($photoName)
    {
        $this->photo_name = $photoName;
        return $this;
    }

    /**
     * Get photoName
     *
     * @return string $photoName
     */
    public function getPhotoName()
    {
        return $this->photo_name;
    }

    public function setPhotoFile($file)
    {
        $this->photo_file = $file;
        return $this;
    }

    public function getPhotoFile()
    {
        return $this->photo_file;
    }

    /**
     * @MongoDB\PrePersist
     */
    public function setUpdatedAtValue()
    {
        $this->setUpdatedAt(new \DateTime());
    }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

也许这是php设置的情况? 增加upload_max_filesize = 2M中的php.ini值会有所帮助。