0

嗨,我已经创建了一个文件上传表单,除了当我按下提交它不会将我重定向到 Uploads/add.ctp 之外,它都可以正常工作,但它确实将文件保存到目录和数据库中。事实上,如果我将重定向指向上传/浏览,它仍然不会带我去上传/浏览。

这是我的控制器

public function add() {

if(!empty($this->data)){

    $file = $this->request->data['Upload']['file'];
if ($file['error'] === UPLOAD_ERR_OK && $this->Upload->save($this->data)){
    $this->Upload->save($this->data);
    if(move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) {
    $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));
    $this->redirect(array('controller'=>'Uploads','action' => 'add'));
    } else{

    $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));

    }
} 


}
}

这是我的表格

      <div class="maincontent">
      <?php echo $this->Form->create('Upload', array('type' => 'file', 'class'=>'uploadfrm'));?>
<fieldset class='registerf'>
    <legend class='registerf2'>Upload a Video</legend>
<?php
    echo 'Upload your video content here, there is no size limit however it is       <b>.mp4</b> file format only.';
    echo '<br/>';
    echo '<br/>';
    echo $this->Form->input('name', array('between'=>'<br />', 'class'=>'input'));
    echo $this->Form->input('eventname', array('between'=>'<br />'));
    echo $this->Form->input('description', array('between'=>'<br />', 'rows'=> '7', 'cols'=> '60'));
    echo  $this->Form->hidden('userid', array('id' => 'user_id','value' => $auth['id']));
    echo $this->Form->hidden('username', array('id' => 'username', 'value' => $auth['username']));
    echo $this->Form->input('file', array('type' => 'file'));
    echo "<br/>"
?>
<?php echo $this->Form->end(__('Submit', true));?>
</fieldset>

     <?php

   class UploadsController extends AppController {
   public $name = 'Uploads';

      public $helpers = array('Js');



// Users memeber area, is User logged in…
 public $components = array(
    'Session',
    'RequestHandler',
    'Auth'=>array(
        'loginRedirect'=>array('controller'=>'uploads', 'action'=>'browse'),
        'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'),
        'authError'=>"Members Area Only, Please Login…",
        'authorize'=>array('Controller')
    )
  );


       public function isAuthorized($user) {
            // regular user can access the file uploads area
        if (isset($user['role']) && $user['role'] === 'regular') {
            return true;
             }

             // Default deny
                return false;
            }





function index() {
        $this->set('users', $this->Upload->find('all'));

}




// Handling File Upload Function and updating uploads database


    public function add() {

        if(!empty($this->data)){

            $file = $this->request->data['Upload']['file'];
            if ($file['error'] === UPLOAD_ERR_OK){
            $this->Upload->save($this->data); 
                if(move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) 
                {
                    $this->redirect(array('controller' => 'Uploads', 'action' => 'add'));
                    $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));


                    }   }else {

                        $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));

                            }

                }
                }   

function browse () {
        // Find all in uploads database and paginates
$this->paginate = array(
    'limit' => 5    ,
    'order' => array(
        'name' => 'asc'
    )
    );
    $data = $this->paginate('Upload');
    $this->set(compact('data'));


     }

function recentuploads () {
$uploads =  $this->Upload->find('all', 
    array('limit' =>7,
    'order' => 
        array('Upload.date_uploaded' => 'desc')));
    if(isset($this->params['requested'])) { 
         return $uploads; 
         } 
            $this->set('uploads', $uploads); 
}

function watch ($id = null){
$this->set('isAjax', $this->RequestHandler->isAjax());

    // Read Uploads Table to watch video
    $this->Upload->id = $id;      
    $this->set('uploads', $this->Upload->read());



    // Load Posts Model for comments related to video
            $this->loadModel('Post');
    $this->paginate = array(
                'conditions' => array(
                'uploadid' => $id),
                'limit' => 4
                );

    $data = $this->paginate('Post');
    $this->set(compact('data'));






// Load Likes Model and retrive number of likes and dislikes

    $this->loadModel('Like');

    $related_likes = $this->Like->find('count', array(
        'conditions' => array('uploadid' => $id)
    ));
    $this->set('likes', $related_likes);
    }




}
    ?>

有什么建议么?

4

2 回答 2

1

add功能在您的 UploadsController 中,对吗?并且您希望它重定向到上传/浏览?

在您的 UploadsController 中,$name设置为什么?

<?php
class UploadsController extends AppController {
   public $name = ?; // What is this variable set to?
}

通过 Cake 的 Inflector,当你在重定向中指定控制器时,它应该是小写的:

$this->redirect(array('controller' => 'uploads', 'action' => 'browse'));

或者,如果您引导的动作和要引导的动作在同一个控制器中,您甚至不需要指定控制器。例如,如果您从 UploadsController 提交表单add()并希望重定向到browse()

$this->redirect(array('action' => 'browse'));

试试看是否有帮助。

另请注意,您在add函数中调用了 $this->Upload->save($this->data) 两次。

public function add() {

    if(!empty($this->data)){
        $file = $this->request->data['Upload']['file'];
        if ($file['error'] === UPLOAD_ERR_OK && $this->Upload->save($this->data)) {
            $this->Upload->save($this->data);
            if(move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) {
                $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));
                $this->redirect(array('controller'=>'Uploads','action' => 'add'));
            } else {
                $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));
            }
        }
    } 

}

具体来说,这里:

if ($file['error'] === UPLOAD_ERR_OK && $this->Upload->save($this->data)) {
    $this->Upload->save($this->data);
    ...

当您在if条件中调用它时,它仍然将数据保存到数据库中。去掉第二个就好了。

于 2012-04-14T19:20:43.423 回答
0

如果我在函数中添加以下行添加

            $this->render();

一切都很完美,我一直在为自己的生活而奋斗,以弄清楚为什么我必须渲染视图,如果所有其他视图都默认渲染的话。但无论如何让它工作!希望这对其他人有帮助:)

于 2012-04-28T15:40:09.743 回答