1

とても単純なものがとても難しいことがあるのはとてもクレイジーです。

すべてのcakephp2.1の達人に電話してください。フォームがあり、名前とテキスト画像(var)フィールドがあります。

画像を投稿したいのですが、サイズ変更、サムネイル、特別なことは何もありません。画像を投稿してview.ctpで表示する簡単なMVCの例です。

それでおしまい。私はcakephp.orgの/en/2.1ブックとAPIを調べましたが、フォルダーにアップロードしてctpで表示する限り、mvcを機能させることができないようです。

私のコントローラー

public function add() {
    if ($this->request->is('post')) {
        $this->ListShExPainImage->create();
        if ($this->ListShExPainImage->save($this->request->data)) {
            $this->Session->setFlash(__('The list sh ex pain image has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The list sh ex pain image could not be saved. Please, try again.'));
        }
    }
}

public function view($id = null) {
    $this->ListShExPainImage->id = $id;
    if (!$this->ListShExPainImage->exists()) {
        throw new NotFoundException(__('Invalid list sh ex pain image'));
    }
    $this->set('listShExPainImage', $this->ListShExPainImage->read(null, $id));
}

私のadd.ctp

<?php echo $this->Form->create('ListShExPainImage');?>
<fieldset>
    <legend><?php echo __('Add List Sh Ex Pain Image'); ?></legend>
<?php
    echo $this->Form->input('name');
    echo $this->Form->input('image', array('type' => 'file'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>

私のview.ctp

<h2><?php  echo __('List Sh Ex Pain Image');?></h2>
<dl>
    <dt><?php echo __('Id'); ?></dt>
    <dd>
        <?php echo h($listShExPainImage['ListShExPainImage']['id']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Name'); ?></dt>
    <dd>
        <?php echo h($listShExPainImage['ListShExPainImage']['name']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Image'); ?></dt>
    <dd>
        <?php echo h($listShExPainImage['ListShExPainImage']['image']); ?>
        &nbsp;
    </dd>
</dl>

よろしくお願いします。

4

2 に答える 2

1

コントローラーでは、ファイルのアップロードで何もしていません。Cake は時々魔法のように機能しますが、そのアップロードで何をしたいのかを自動的に推測することは、それらの 1 つではありません。

に記載されているように、ファイル アップロード フィールドは、複数のキーを持つデータ配列を返します。nameキーを取得してフィールドとして保存する必要がありimageます(それがあなたが呼び出しているようです)。例えば:

$filename = $this->request->data['ListShExPainImage']['image']['name'];

// Move the image
move_uploaded_file(
    $this->request->data['ListShExPainImage']['image']['tmp_name'],
    '/path/to/your/image/dir/' . basename($filename)'
);

// Set the filename in the database
$this->request->data['ListShExPainImage']['image'] = $filename;

さらに、フォームの作成オプションにファイル タイプを追加して、画像のアップロードを解析できるようにするために、いわゆるmultipart/form-dataenctype をフォームに設定する必要があります。

また、ファイルを一時フォルダーから最終的な場所に移動することを忘れないでください。

于 2012-05-18T22:56:02.430 に答える
0

また、フォームに「タイプ」を追加する必要があります。

<?php echo $this->Form->create('ListShExPainImage', array('type' => 'file'));?>

次に、コントローラーで次のようにします。

if (is_uploaded_file($data['ListShExPainImage']['image']['tmp_name']))
{
    move_uploaded_file(
        $this->request->data['ListShExPainImage']['image']['tmp_name'],
        '/path/to/your/image/dir/' . $this->request->data['ListShExPainImage']['image']['name']
    );

    // store the filename in the array to be saved to the db
    $this->request->data['ListShExPainImage']['image'] = $this->request->data['ListShExPainImage']['image']['name'];
}

次に、作成と保存のルーチンを実行します。

あなたのビューでは、画像を表示するために次のようなことをします:

<?php echo $this->Html->image('/path/to/your/image/dir/' . $listShExPainImage['ListShExPainImage']['image']); ?>
于 2012-05-18T22:59:56.993 に答える