gpt4 book ai didi

php - CakePHP 2.0,提交表单重定向未正确呈现

转载 作者:行者123 更新时间:2023-11-29 14:30:16 24 4
gpt4 key购买 nike

您好,我已经创建了一个文件上传表单,除了当我按提交时,它一切正常,它不会将我重定向到 Uploads/add.ctp,但它确实将文件保存到目录并保存到事实上,如果我将重定向指向上传/浏览,它仍然不会带我去上传/浏览。

这是我的 Controller

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);
}




}
?>

有什么建议吗?

最佳答案

add 函数位于您的 UploadsController 中,对吗?您希望它重定向到上传/浏览吗?

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

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

通过 Cake 的 Inflector,当您在重定向中指定 Controller 时,它应该是小写:

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

或者,如果您定向的操作和您想要定向的操作位于同一 Controller 中,则您甚至不需要指定 Controller 。例如,如果您从 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 条件中调用它时,它仍然会将数据保存到数据库中。把第二个去掉就可以了。

关于php - CakePHP 2.0,提交表单重定向未正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10144701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com