图片没有上传到我们给出的文件夹中

时间:2014-03-17 06:31:24

标签: cakephp

控制器/ imagesController.php

<?php 
class ImagesController extends AppController
{
public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');


    var $uses=array('Images');

    public function index(){

    //  $this->set('posts', $this->carrier->find('all'));
        if($this->request->is('post')){
             Configure::read();
             pr($this->data); 
            $this->Images->create();
            $filename = null;


if (
    !empty($this->request->data['Images']['fname']['tmp_name'])
    && is_uploaded_file($this->request->data['Images']['fname']['tmp_name'])
) {
    // Strip path information
    $filename = basename($this->request->data['Images']['fname']['name']); 
    pr($filename);
    move_uploaded_file( $this->data['Images']['fname']['name'],WWW_ROOT . DS . 'UserImages' . DS . $filename  );

//$this->data['Images']['fname'] = $filename;
}

pr($filename);
// Set the file-name only to save in the database
//pf_($fiename);
$this->request->data['Images']['fname'] = $filename;
$uid = $this->Auth->user('id');
$this->request->data['Images']['uid'] = $uid;
//pf_($this->request->data);
if ($this->Images->save($this->request->data)) {
  pr($this->request->data);
   // ...
     /*if ($this->Carrier->save($this->request->data)) {
                 if ($this->Carrier->save($this->data)) {
                 */
                $this->Session->setFlash(__('Your Details has been saved.'));
                 return $this->redirect(array('controller'=>'images','action' => 'index'));
            } else {
                $this->Session->setFlash(__('Unable to add your Details'));
            }
            }
        }



public function isAuthorized($user) {
    // All registered users can add posts
    if ($this->action === 'index') {
        return true;
    }




    return parent::isAuthorized($user);
}
}
?>

查看/图像/ index.ctp

<?php echo $this->Form->create('Images', array('enctype' => 'multipart/form-data'));?>
<?php echo $this->Form->input('Images.fname', array('between'=>'<br />','type'=>'file'));?>

<?php echo $this->Form->end('Apply');?> 

此处文件上传的代码显示为sucussful,文件名在db中插入,但原始图像未上传到 UserImages 文件夹中。 为此检查但我没有找到解决方案,请帮助我

2 个答案:

答案 0 :(得分:2)

$this->data['Images']['fname']['name']仅包含文件名。 $this->data['Images']['fname']['tmp_name']包含文件数据,应与move_uploaded_file();

一起使用

所以你应该改变

move_uploaded_file($this->data['Images']['fname']['name'], WWW_ROOT.DS.'UserImages'.DS.$filename)

move_uploaded_file($this->data['Images']['fname']['tmp_name'], WWW_ROOT.DS.'UserImages'.DS.$filename)

您还可以对move_uploaded_file()

进行检查
if(move_uploaded_file($this->data['Images']['fname']['tmp_name'], WWW_ROOT.DS.'UserImages'.DS.$filename)) {
    //continue
} else {
    //show error
}

答案 1 :(得分:-2)

在cakephp中使用move_uploaded_file()总是一个不好的做法...因为cakephp总是为图像上传提供一个ImageBehavior ...这会照顾你的图像,文件夹&amp;图像验证也.... 如果您不了解图像行为,可以从这里学习..... http://cakephplogics.blogspot.in/2014/07/cakephp-image-upload-behavior.html