2017-12-23 20 views
0

왜 그런지 모르겠지만 두 개의 업로드 (미리보기, 세부 사항)를 지정하는 형식이 있습니다. 메신저가 저장하려고하면 세부 사항이 저장되지만 미리보기가 아닙니다.yii2 saveAs 두 파일은 하나만 저장합니다.

const UPLOAD_FILE_URL = 'uploads/recipes/'; 
const UPLOAD_FILE_DETAILS_URL = self::UPLOAD_FILE_URL.'details/';  

$filePath = self::UPLOAD_FILE_URL . $slug . '.' . $this->imageSrc->extension; 
      $filePathDetail = self::UPLOAD_FILE_DETAILS_URL . $slug . '.' . $this->imageDetailSrc->extension; 

      if ($this->imageSrc->saveAs($filePath) && $this->imageDetailSrc->saveAs($filePathDetail)) { 
       $this->imageSrc = $slug . '.' . $this->imageSrc->extension; 
       $this->imageDetailSrc = $slug . '.' . $this->imageDetailSrc->extension; 
      } 

      if ($this->save(false, ['imageSrc', 'imageDetailSrc'])) { 
       return true; 
      } 

답변

1

Yii2 공식 문서를 다른 이름으로 저장 기능은 임시 파일을 이동 인 move_uploaded_file() 함수를 사용한다고 열 imageSrc에 $ 슬러그를 저장 imageDetailSrc - DB는 내가 예상 한대로 결과가 있습니다. 따라서 임시 파일은 saveAs 함수를 처음 호출 할 때 삭제됩니다. saveAs가 삭제하지 않게하려면 saveAs 함수의 두 번째 매개 변수로 false를 보내야합니다.

const UPLOAD_FILE_URL = 'uploads/recipes/'; 
const UPLOAD_FILE_DETAILS_URL = self::UPLOAD_FILE_URL.'details/';  

$filePath = self::UPLOAD_FILE_URL . $slug . '.' . $this->imageSrc- 
>extension; 
$filePathDetail = self::UPLOAD_FILE_DETAILS_URL . $slug . '.' . $this->imageDetailSrc->extension; 

if ($this->imageSrc->saveAs($filePath, false) && $this->imageDetailSrc->saveAs($filePathDetail)) { 
     $this->imageSrc = $slug . '.' . $this->imageSrc->extension; 
     $this->imageDetailSrc = $slug . '.' . $this->imageDetailSrc->extension; 
} 
if ($this->save(false, ['imageSrc', 'imageDetailSrc'])) { 
    return true; 
}