2

libraries/joomla/filesystem/file.php に基づくカスタム フィールドのアップロード メカニズムの作成に問題があります。フォームには、アップロードしていないビューに正しい enctype が設定されています。コンポーネント/モデル/フィールド/uploadfield.php のコードは次のとおりです。

protected function getInput()
{

    //Retrieve file details from uploaded file, sent from upload form
    $file = JFactory::getApplication()->input->get($this->name, null, 'files', 'array');

    //Import filesystem libraries. Perhaps not necessary, but does not hurt
    jimport('joomla.filesystem.file');

    //Clean up filename to get rid of strange characters like spaces etc
    $filename = JFile::makeSafe($file['name']);

    //Set up the source and destination of the file
    $src = $file['tmp_name'];
    $dest = JPATH_COMPONENT . DIRECTORY_SEPARATOR . $filename;

    //First check if the file has the right extension, we need jpg only
    if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
        if ( JFile::upload($src, $dest) ) {
        //Redirect to a page of your choice
        } else {
        //Redirect and throw an error message
        }
    } else {
    //Redirect and notify user file is not right extension
    }

    return '<input type="file" name="' . $this->name . '" id="' . $this->id . '"' . ' />';  
}

getInput() 関数でアップロードメカニズムを使用して、これを正しい方法で行っていますか? それはモデルにあるべきですか?http://docs.joomla.org/How_to_use_the_filesystem_packageに従うことを試みてきましたが、アップロードコードがどこに行くべきかを言うことを怠っていますか?

どうもありがとう

4

2 に答える 2

3

コンポーネントの1つで使用したものを使用してみてください。

function getInput(){

    jimport('joomla.filesystem.file');

    $jinput = JFactory::getApplication()->input;
    $fileInput = new JInput($_FILES);
    $file = $fileInput->get('image', null, 'array');

    if(isset($file) && !empty($file['name'])) { 
        $filename = JFile::makeSafe($file['name']);
        $src = $file['tmp_name'];
        $data['image']=$filename;

        $dest = JPATH_COMPONENT . '/' . $filename;

        if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
            if(!JFile::upload($src, $dest)) {
            return false;
            }
        }
        else {
            JError::raiseWarning('', 'File type not allowed!.');
            return false;
        }
    }
    return true;
}

次のコードで注意してください:

$file = JRequest::getVar('image', null, 'files', 'array');

画像」は、次のように入力フィールドの名前から取得されます。

<input type="file" id="" name="image" />

したがって、入力フィールドに付けた名前に変更してください。

于 2012-12-12T16:08:49.190 に答える
1

わかりました-だから私は問題を解決しました:

カスタム フィールド ファイルで getInput() 関数を使用して、フォームに入力フィールドを表示するジョブを実行し、コントローラーで関数を作成して保存を行いました。次のようになります。

function save(){
    jimport('joomla.filesystem.file');

    $jFileInput = new JInput($_FILES);
    $theFiles = $jFileInput->get('jform',array(),'array');

    $filepath = "DESTINATIONFILEPATH"
    JFile::upload( $theFiles['tmp_name']['filename'], $filepath );

    return parent::save();
}

ファイル名がデータベースに更新されるように、ファイル名をJInputの「jform」配列に保存するJoomla 3.0の方法を考え出す必要があります。これは、既存の jform データを上書きするだけです。

$jinput = JFactory::getApplication()->input;
$jinput->set('jform',$arraytoadd);

誰かが興味を持っている場合は、ここで質問しました: Adding field input to JForm using JInput

于 2013-01-03T18:56:52.560 に答える