本文实例分析了symfony2使用第三方库upload制作图片上传的方法。分享给大家供大家参考,具体如下:
我们在应用程序或者网站的个人资料里一般都有设置头像的功能,这一章我们在symfony2里用第三方的一个比较有名upload库来制作上传图片的功能。
一、安装第三方库
1.在composer.json文件中的”require”中加入
"codeguy/upload": "*"
2.运行指令安装
composer update
二、编码
1.编写uploadpic方法上传图片,并将上传图片的用户id作为文件名
<?php
/**
* @author sun
* by blogs.zmit.cn http://blogs.zmit.cn
* 原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处 http://blogs.zmit.cn/6544.html
* 中梦博客,作者信息和本声明。否则将追究法律责任。
*/
namespace zmadminbundlecontroller;
use symfonybundleframeworkbundlecontrollercontroller;
use symfonycomponentfilesystemfilesystem;
class defaultcontroller extends controller {
public function indexaction($name) {
return $this->render('zmadminbundle:default:index.html.twig', array('name' => $name));
}
/**
* 上传图片
*
* @param type $user_id 用户的id,用作文件名
* @param type $str 表单中file类型的input的name
* @param type $path 保存路径
* @return type
*/
public function uploadpic($user_id, $str, $path) {
$fs = new filesystem();
//检查路径是否存在
if (!$fs->exists($path)) {
//如果不存在,创建目录
$fs->mkdir($path, 0700);
}
//使用upload库
$storage = new uploadstoragefilesystem($path);
$file = new uploadfile($str, $storage);
//如果文件名为空
if ($file->getname() != '') {
//设置文件名为用户的id
$file->setname($user_id);
//验证文件上传
$file->addvalidations(array(
//指定文件类型
new uploadvalidationmimetype(array('image/png', 'image/jpg', 'image/jpeg', 'image/gif')),
//指定文件大小
new uploadvalidationsize('2m')
));
//上传文件
try {
//成功
$file->upload();
//文件名和扩展名
$file_name = $file->getnamewithextension();
} catch (exception $e) {
//失败!
$errors = $file->geterrors();
}
}
//返回文件名和扩展名
return $file_name;
}
}
2.用户上传头像,并将头像全路径存入数据库表
<?php
/**
* 联系人控制器
* @author sun
* by blogs.zmit.cn http://blogs.zmit.cn
* 原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处 http://blogs.zmit.cn/6544.html
* 中梦博客,作者信息和本声明。否则将追究法律责任。
*/
namespace zmapibundlecontroller;
//引用写好的上传图片方法uploadpic的controller,并命名为basecontroller
use zmadminbundlecontrollerdefaultcontroller as basecontroller;
use symfonycomponenthttpfoundationrequest;
use symfonycomponenthttpfoundationresponse;
//继承basecontroller
class contactcontroller extends basecontroller {
/**
* 用户上传头像
*
* @return response
*/
public function uploadheadaction() {
$request = request::createfromglobals()->request;
$user_id = $request->get('user_id');
//判断是否有文件上传
if (isset($_files['head']) && $_files['head'] != '') {
$conn = $this->getdoctrine()->getconnection();
$data = $conn->fetchassoc("select id, head from contact where id = ? limit 1", array($user_id));
//判断用户是否存在
if(!empty($data['id'])) {
//设置图片保存路径
$path = 'image/head/';
//获取上传文件后返回的文件名和扩展名
$file_name = $this->uploadpic($user_id, 'head', $path);
//修改用户contact表head头像字段的值
$conn->executeupdate("update contact set head = ? where id = ?", array($path . $file_name, $user_id));
$result['flag'] = 1;
$result['content'] = '上传头像成功!';
} else {
$result['flag'] = 3;
$result['content'] = '用户不存在!';
}
}else{
$result['flag'] = 2;
$result['content'] = '上传失败,没有选择图片!';
}
return new response(json_encode($result), '200', array('content-type' => 'application/json'));
}
}
这样图片就上传成功,将用户的id作为文件名,并修改表字段值为图片的全路径
本文永久地址:http://blog.it985.com/6544.html
本文出自 it985博客 ,转载时请注明出处及相应链接。
更多关于php框架相关内容感兴趣的读者可查看本站专题:《php优秀开发框架总结》,《codeigniter入门教程》,《ci(codeigniter)框架进阶教程》,《yii框架入门及常用技巧总结》及《thinkphp入门教程》
希望本文所述对大家基于symfony框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!