如何在cakephp中保存图像
我需要上传表格和文件夹中的图像。请帮助我,如何将图像保存在文件夹和数据库中。请描述一下方法 谢谢 Manish您可以在PHP中“像往常一样”进行操作。我几天前刚做过这样的事情:如何在cakephp中保存图像,php,cakephp,Php,Cakephp,我需要上传表格和文件夹中的图像。请帮助我,如何将图像保存在文件夹和数据库中。请描述一下方法 谢谢 Manish您可以在PHP中“像往常一样”进行操作。我几天前刚做过这样的事情: $path = "/img/avatars/"; $dir = getcwd().$path; $avatarFile = "$dir$id.png"; if (isset($this->data['User']['avatar']) && $this->data['User']['avat
$path = "/img/avatars/";
$dir = getcwd().$path;
$avatarFile = "$dir$id.png";
if (isset($this->data['User']['avatar']) && $this->data['User']['avatar']['error'] == 0) {
$avatar = $this->data['User']['avatar'];
if (!is_uploaded_file($avatar['tmp_name'])) $this->Utils->panic($avatar);
if (in_array($avatar['type'], array('image/jpeg','image/pjpeg','image/png'))) {
// load image
list($width, $height) = getimagesize($avatar['tmp_name']);
$width = $height = min($width, $height);
if (in_array($avatar['type'], array('image/jpeg','image/pjpeg')))
$source = imagecreatefromjpeg($avatar['tmp_name']);
else
$source = imagecreatefrompng($avatar['tmp_name']);
// resize
$thumb = imagecreatetruecolor(128, 128);
imagecopyresized($thumb, $source, 0, 0, 0, 0, 128, 128, $width, $height);
// save resized & unlink upload
imagepng($thumb, $avatarFile);
$success &= true;
} else {
$this->User->invalidate('avatar', __("Only JPG or PNG accepted.",true));
$success &= false;
}
unlink($avatar['tmp_name']); // Delete upload in any case
}
它甚至会调整它的大小为128x128,你可以跳过它,只需将上传的图像重命名为目标目录。谷歌也会帮你解决这个问题,上传文件没有什么特别的
上传表格:
echo $form->create('User', array(
'enctype' => 'multipart/form-data',
'type' => 'post',
));
echo $form->input('avatar', array('type' => 'file', 'label' => __('Avatar:',true)));
附加信息:大部分上传的文件存储在文件系统中,并在数据库中引用它(文件名)。不要在数据库中存储文件。在我的情况下,我没有在数据库中存储任何内容,因为每个用户只能有一个头像,所以我只覆盖现有文件,不需要在数据库中引用。感谢您热情的回复。。。。。。。我想上传文件夹中的图像,而不是在数据库中。如果你有代码,请帮助我,目前我使用的是cakephp的1.3版本,感谢Salmost可以复制你的例子。谢谢