1

SilverStripe 2.4.7を使用していますが、FileIFrameFieldでアップロードしたファイルを解析するメソッドを追加したいと思います。私が困惑しているのは、これをどこに置くかです。onAfterWriteメソッドを考えていましたが、ファイルは残りのフィールドが初めて保存された後にのみアップロードされるため、これが機能するかどうかはわかりません。

私の質問は:この種のもののベストプラクティスは何ですか?

編集

$ filenameがアップロードされたファイルへのパスであるこのコード行がありますが、「そのようなファイルまたはディレクトリのエラーはありません」というエラーが発生し続けます。ファイルパスにハードコーディングしようとしましたが、同じエラーが発生します。

$fh = fopen($filename, 'r');
4

1 に答える 1

1

新しいファイルを解析する最善の方法は、uploadfield の save メソッドにフックすることです。

(SilverStripe 3 には UploadField と呼ばれる新しいクラスがあり、 UploadField では上書きする必要がありUploadField->upload(SS_HTTPRequest $request)、そこにあるファイルは次のようにアクセスできます: $tmpfile = $request->postVar($this->getName());)

以下、および FileIframeField でそれを行う方法の例:

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();
    } 
}
于 2012-10-06T09:23:07.937 に答える