<?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);
}
}
?>
<?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');?>
这里的文件上传代码显示可疑和文件名在db中inserterd,但原始图像未上传到UserImages文件夹中..对此进行了检查,但我没有找到解决方法,请帮助我
$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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句