symfony2图像未上传到服务器端

时间:2014-09-22 05:43:52

标签: symfony image-uploading sonata-admin

我目前正在使用symfony2 + sonata admin bundle将图片上传到服务器。但是它在本地机器上的成功,它在服务器上失败了,文件没有上传到服务器上。有人请帮忙!

// image.php

class Image
{
/**
 * @var integer
 */
public $id;

/**
 * @var string
 */
public $name;

/**
 * @var integer
 */
public $ord;

/**
 * @var \Ibase\StoreBundle\Entity\Product
 */
private $Product;

/**
 * @var integer
 */
public $path;

/**
 * Set id
 *
 * @param integer $id
 * @return Image
 */
public function setId($id)
{
    $this->id = $id;

    return $this;
}

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

/**
 * Set name
 *
 * @param string $name
 * @return Image
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * Set ord
 *
 * @param integer $ord
 * @return Image
 */
public function setOrd($ord)
{
    $this->ord = $ord;

    return $this;
}

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

/**
 * Set Product
 *
 * @param \Ibase\StoreBundle\Entity\Product $product
 * @return Image
 */
public function setProduct(\Ibase\StoreBundle\Entity\Product $product = null)
{
    $this->Product = $product;

    return $this;
}

/**
 * Get Product
 *
 * @return \Ibase\StoreBundle\Entity\Product 
 */
public function getProduct()
{
    return $this->Product;
}

//const SERVER_PATH_TO_IMAGE_FOLDER ='';

/**
 * Unmapped property to handle file uploads
 */
private $file;

/**
 * Sets file.
 *
 * @param UploadedFile $file
 */
public function setFile(UploadedFile $file = null)
{
    $this->file = $file;
}

/**
 * Get file.
 *
 * @return UploadedFile
 */
public function getFile()
{
    return $this->file;
}

/**
 * Manages the copying of the file to the relevant place on the server
 */
public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->getFile()) {
        return;
    }

    // we use the original file name here but you should
    // sanitize it at least to avoid any security issues

    // move takes the target directory and target filename as params
    $this->getFile()->move(
        $this->getUploadRootDir(),
        $this->getFile()->getClientOriginalName()
    );

    // set the path property to the filename where you've saved the file
    $this->path = $this->getFile()->getClientOriginalName();

    // clean up the file property as you won't need it anymore
    $this->setFile(null);
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded
    // documents should be saved
    return __DIR__.'/../../../../web/'.$this->getUploadDir();
}

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn’t screw up
    // when displaying uploaded doc/image in the view.
    return '/uploads';
}

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

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

/**
 * @ORM\PrePersist
 * Lifecycle callback to upload the file to the server
 */
public function lifecycleFileUpload() {
    $this->upload();
}

/**
 * Updates the hash value to force the preUpdate and postUpdate events to fire
 */
public function refreshUpdated() {
//$this->setUpdated(date('Y-m-d H:i:s'));
}

/**
 * @var \DateTime
 */
private $updated;

/**
 * Set updated
 *
 * @param \DateTime $updated
 * @return Image
 */
public function setUpdated($updated)
{
    $this->updated = $updated;

    return $this;
}

/**
 * Get updated
 *
 * @return \DateTime 
 */
public function getUpdated()
{
    return $this->updated;
}

public function __toString()
{
    return $this->getName() ? $this->getName() : "";
}

// imageAdmin.php(奏鸣曲)

class ImageAdmin extends Admin {

 protected $parentAssociationMapping = 'product';

 protected function configureFormFields(FormMapper $formMapper)
{

    $formMapper
      ->add('file', 'file', array('required' => false))
      ->add('name')
      ->add('Product', 'sonata_type_model', array('attr'=>array("hidden" => true)), array());  
 }


public function prePersist($image) {
    $this->manageFileUpload($image);
}

public function preUpdate($image) {
    $this->manageFileUpload($image);
}

private function manageFileUpload($image) {
    if ($image->getFile()) {
        $image->refreshUpdated();
    }
}

 /**
 * @param \Sonata\AdminBundle\Show\ShowMapper $showMapper
 *
 * @return void
 */
protected function configureShowFields(ShowMapper $showMapper)
{
     $showMapper
        ->add('id')
        ->add('images')
        ->add('name');
}

/**
 * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
 * @return void
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('id')
        ->add('images','images', array('required'=>false, 
            'template' => 'IbaseStoreBundle:Admin:list_images.html.twig'))
        ->add('name')
        ->add('product')
    ;
}

抱歉,我知道它有点长,但是没有弹出任何错误,文件没有上传到服务器文件夹"上传"。

2 个答案:

答案 0 :(得分:2)

您的系统没有文件创建权限,无法创建上传目录。

你可以试试这个:

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn’t screw up
    // when displaying uploaded doc/image in the view.
    $directory = '/uploads';
    if(!file_exists($directory)) {
        mkdir("/uploads", 0777);
      }

    return '/uploads';
}

答案 1 :(得分:0)

我终于明白了什么是错的。我有两种不同回报的方法:

protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}

protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn’t screw up
// when displaying uploaded doc/image in the view.
return '/uploads';
}

但在我的服务器端,上传目录的位置与我的本地主机不同。图像已上传,但在其他地方。