2012-09-20 2 views
1

저는 SilverStripe 2.4.7을 사용하고 있으며 방금 FileIFrameField를 사용하여 업로드 한 파일을 구문 분석하는 메서드를 추가하려고합니다. 나를 곤란하게 만든 것은 이걸 어디에 둘 것인지입니다. onAfterWrite 메서드를 생각하고 있었지만 파일의 나머지 부분을 처음으로 저장 한 후에 업로드하면이 파일이 제대로 작동하는지 잘 모르겠습니다.구문 분석이 포함 된 FileIFrameField

제 질문은 이런 종류의 모범 사례는 무엇입니까?

편집

나는 $ 파일 이름은 업로드 된 파일의 경로를 인 코드 줄을 가지고 있지만 나는 "그런 파일 또는 디렉터리 오류"점점 않습니다 유지. 나는 심지어 파일 경로에서 하드 코딩을 시도했지만 동일한 오류가 발생합니다.

$fh = fopen($filename, 'r'); 
+0

그래서 sittree 객체 또는 일반 데이터 객체에 대해 complextablefield를 통해 관리하고 있습니까? 행동은 다르다, 나는 생각한다. '페이지'가 즉시 sitetree에 생성 될 때 db에 저장된다. – schellmax

+0

아니요. 죄송합니다. 큰 덩어리의 정보를 남겨 두지 않았습니다. DataObjectManager 및이 업로드 필드가있는 DataObjecta 중 하나를 사용하고 있습니다. – MillyMonster

+0

당신은 당신이 그 파일로 무엇을하고 싶은지에 대해 자세하게 설명 할 수 있습니다. 나는 당신이하려고하는 것을 볼 수 없기 때문에 그것을하기가 어렵습니다. – Zauberfisch

답변

1

저장 방법 uploadfield에 훅하는 것입니다 새 파일을 구문 분석하는 가장 좋은 방법은 FileIframeField을 위해 당신이 할 수있는 하위 그것을 클래스라는 저장 덮어 쓰기하여()

(이 SilverStripe 3 FileIframeField에서 작업을 수행하는 방법에 대한

아래

, 예) $tmpfile = $request->postVar($this->getName()); : UploadField라는 새로운 클래스, UploadField 당신이 UploadField->upload(SS_HTTPRequest $request)을 덮어해야하고 파일은 다음과 accesable이있을 것입니다

class myFileIFrameField extends FileIFrameField { 
    public function save($data, $form) { 
     if (
      !isset($data['FileSource']) 
      || ($data['FileSource'] == 'new' && (!isset($_FILES['Upload']) || !$_FILES['Upload'])) 
      || ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile'])) 
     ) { 
      $form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required'); 
      Director::redirectBack(); 
      return; 
     } 
     $fileContent = false; 
     if($data['FileSource'] == 'new') { 
      $fileContent = file_get_contents($_FILES['Upload']['tmp_name']); 
     } 
     elseif($data['FileSource'] == 'existing') { 
      $fileObject = DataObject::get_by_id('File', $data['ExistingFile']); 
      $fileContent = file_get_contents($fileObject->getFullPath()); 
     } 
     if ($fileContent) { 
      // parse the $fileContent here 
     } 
     // if you want to still save the file into a relation, 
     //meaning if you want to have the actually FileIframeField behaviour still in tact then call 
     return parent::save($data, $form); 
     // other wise, if you do not want to save the relation and you don't want to save the file to the server 
     // thenn do NOT call parent::save, just do: 
     // Director::redirectBack(); 
    } 
}