在CakePHP中上载和检索图像
我有以下代码来用cakephp上传一个文件在CakePHP中上载和检索图像,cakephp,upload,store,Cakephp,Upload,Store,我有以下代码来用cakephp上传一个文件 class UsersController extends AppController { var $name = 'Users'; function index() { $this->set('users', $this->User->find('all')); } function add() { if (!empty($this->data)) {
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::代码应该完全用CakePHP编写
请帮忙,
提前感谢请确保用于上载文件的文件夹具有足够的权限,并且您可以使用Cakephp Media view下载文件。请检查此项以供参考。 在函数下载中使用图像名称或图像id作为参数,并将路径更改为 '路径'=>应用程序。'上传'。DS
“uploads”文件夹应位于webroot目录中 确保用于上载文件的文件夹具有足够的权限,并且您可以使用Cakephp Media view下载文件。请检查此项以供参考。 在函数下载中使用图像名称或图像id作为参数,并将路径更改为 '路径'=>应用程序。'上传'。DS
“uploads”文件夹应位于webroot目录中 谢谢你的帮助@ammuThank你的帮助@ammu