2014-09-22 8 views
0

현재 symfony2 + sonata admin 번들을 사용하여 이미지를 서버에 업로드하고 있습니다. 그러나 그것이 로컬 머신에서 성공했는지, 서버에서 실패했는지, 파일은 서버에 업로드되지 않았다. 누구든지 도와주세요!symfony2 이미지가 서버쪽에 업로드되지 않았습니다.

//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') 
    ; 
} 

죄송합니다, 나는 그것이 조금 긴 알고 있지만, 어떤 오류가 튀어 was't 파일은 서버 폴더 "uploads"로 올라가지 않습니다.

+0

** ** [2014-09-22 02:27:46] request.ERROR : PHP 예외가 발견되었습니다. Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException : "GET/th1s_1s_a_4o4에 대한 경로를 찾을 수 없습니다. html ""/home/ibase258/public_html/app/cache/prod/classes.php 라인 1889 { "예외": "[개체] (Symfony \\ 구성 요소 \\ HttpKernel \\ 예외 \\ NotFoundHttpException : \ "GET /th1s_1s_a_4o4.html \"/home/ibase258/public_html/app/cache/prod/classes.php:1889, Symfony \\ 구성 요소 \\ 라우팅 \\ 예외 \\ ResourceNotFoundException :/home/ibase258/public_html에 있습니다. /app/cache/prod/appProdUrlMatcher.php:1003) "} [] – GaryZ

답변

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'; 
} 

그러나 내 서버 측에서는 업로드 디렉토리의 위치가 로컬 호스트와 다릅니다. 이미지가 업로드되었지만 다른 위치에 있습니다.

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'; 
} 
+0

Thx @ hizbul25하지만 좀 더 구체적으로 말할 수 있습니까? 어디에서 허가를 만드시겠습니까? 나는 symfony2를 처음 접했는데, 덕분에 – GaryZ

+0

안녕하세요, @ hizbul25에 대한 답장을 해봤지만 mkdir()에 대한 권한이 거부되었습니다. 'uploads'폴더는 public_html 폴더를 업데이트합니다. chmod는 안된다고 생각합니다. 그? 권리? 어떻게 생각해? – GaryZ

+0

chmod -R 0777 projectfile – hizbul25

관련 문제