【问题标题】:Cakephp 2x Upload imageCakephp 2x 上传图片
【发布时间】:2017-05-31 00:58:55
【问题描述】:

我正在尝试将图像上传到我的本地文件夹

webroot/img/

并将图像名称保存到数据库。一切似乎都很好,但图像没有保存到路径中

这是我的看法

<?php echo $this->Form->create(null,['url' => ['controller' => 'users', action' => 'update_image'],
array('enctype'=>'multipart/form-data')]); ?>
<?php echo $this->Form->input('User.id') ?>
<?php echo $this->Form->file('Profile.picture',['type'=>'file']) ?>
<?php echo $this->Form->end('submit') ?>

我的控制器

public function update_image(){

    $id = $this->Auth->user('id'); 
    $this->Profile->id = $id; 
    $this->set('profile', $this->User->findById($id)); 

    if ($this->request->is(array('post','put'))) {
    $frmData = $this->request->data;

        //Path to store upload image
        $target = "/teamjob_back/img/../img/".basename($frmData['Profile']['picture']);

        //Get the data from form
        $image = $frmData['Profile']['picture'];

        //save data to database
        $this->Profile->save($this->request->data);

        //Image store to the img folder
        if (move_uploaded_file($image['tmp_name']['name'], $target)) {
            echo "Image successfull";
        }
    }
}

还有代码

$image['tmp_name']['name']

给我一​​个非法字符串偏移错误。

已编辑 此代码适用于控制器

if ($this->request->is('post')) { 
        $frmData = $this->request->data;  
        $tmp = $frmData['picture']['tmp_name']; 
        $hash = rand();
        $date = date("Ymd");
        $image = $date.$hash."-".$frmData['picture']['name'];
        $target = WWW_ROOT.'img'.DS.'uploads'.DS; 

        $target = $target.basename($image); 
        if (move_uploaded_file($tmp, $target)) {
            echo "Successfully moved"; 
        }
        else
        {
            echo "Error";
        }
    }

【问题讨论】:

  • &lt;?= $this-&gt;Form-&gt;create(NULL, ['enctype' =&gt; 'multipart/form-data']) ?&gt;创建表单的时候加enctype了吗?
  • &lt;form action="/teamjob_back/users/picture" enctype="multipart/form-data" id="UserPictureForm" method="post" accept-charset="utf-8"&gt;
  • 该代码有效

标签: php cakephp cakephp-2.9


【解决方案1】:

首先你需要删除 "type => file" 因为你已经使用了 Form helper "file" 功能,如果我们使用 "Form->input" 那么我们使用 'type=>file'

    e.g. $this->Form->input('email', array('type' => 'email'));
$this->Form->file('Profile.picture')

检查你的目标路径是否有写权限,pr($image) 然后你得到 $image['temp_name] 或 $image[0]['temp_name] 等的正确数组格式。 并将您的 temp_name 放入 move_uploaded_file 函数中

e.g. move_uploaded_file(your temp_name)

Form Helper

【讨论】:

    【解决方案2】:

    先添加

    ['type' => 'file']
    

    作为文件的一个选项,而不是尝试发送 enctype。

    第二个$image$this-&gt;request-&gt;data['Profile']['picture'] 所以你不能同时使用['tmp_name']['name'] ... debug($this-&gt;request-&gt;data['Profile']['picture'] 你会发现你可以拥有

    $image['tmp_name']
    

    $image['name']
    

    但不能同时使用。

    可能更多,但这些都是好的开始。

    【讨论】:

    • 我在我的控制器上做了这个move_uploaded_file($image['name'], $target),在我看来也是&lt;?php echo $this-&gt;Form-&gt;create(null,['url' =&gt; ['controller' =&gt; 'users', 'action' =&gt; 'update_image'], array('type'=&gt;'file')]); ?&gt;,但仍然没有任何反应,你能告诉我更多谢谢先生
    • 那不应该工作......你需要 $image['tmp_name'] 到目标,因为这会将 /tmp/image.jdkkd.jpeg 移动到目标.. .. 然后你也想把它保存到数据库中
    • 你可以考虑使用 Proffer 之类的东西。
    【解决方案3】:

    确保在创建表单上传文件时包含 enctype

     <?= $this->Form->create(NULL, ['enctype' => 'multipart/form-data']) ?>
    

    【讨论】:

      猜你喜欢
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多