我有以下代码在cakephp中上传文件
class UsersController extends AppController {
var $name = 'Users';
function index() {
$this->set('users', $this->User->find('all'));
}
function add() {
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your user data has been saved.');
$this->redirect(array('action' => 'index'));
}
$this->User->create();
if ($this->uploads() && $this->User->save($this->data)) {
$this->Session->setFlash(_('Upload saved', true));
} else {
$this->Session->setFlash(_('Upload not saved.Please try again', true));
}
}
}
function uploads() {
$uploads_dir = '/uploads';
$users = $this->request->data['Upload']['file'];
if ($users['error'] === UPLOAD_ERR_OK) {
if (move_uploaded_file($users['User']['file'], $uploads_dir)) {
$this->User->saveAll($this->data);
return true;
}
}
return false;
}
function edit($id = null) {
$this->User->id = $id;
if (empty($this->data)) {
$this->data = $this->User->read();
} else {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your user details has been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
function delete($id) {
$this->User->delete($id);
$this->Session->setFlash('This user has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
当我尝试上传时,文件正在上传,但我需要上传的文件才能显示为每个.jpeg显示的超链接,但我收到的错误为
“在此服务器上找不到请求的地址'/Users/app/webroot/index.php/uploads'。”
另外,请帮助我如何将上传的图像存储在另一个文件夹中
PS :: code应该完全在CakePHP中
请帮忙, 提前致谢
答案 0 :(得分:1)
确保用于上传文件的文件夹具有足够的权限,您可以使用Cakephp Media视图下载文件。供您参考,请检查一下。 Downloading files using media view in CakePHP
在功能下载中使用图像名称或图像ID作为参数,并将路径更改为 'path'=> APP。 '上传'。 DS
“uploads”文件夹应该在webroot目录中。