Symfony2使用第三方库Upload制作图片上传实例详解

2025-05-29 0 69

本文实例分析了symfony2使用第三方库upload制作图片上传的方法。分享给大家供大家参考,具体如下:

我们在应用程序或者网站的个人资料里一般都有设置头像的功能,这一章我们在symfony2里用第三方的一个比较有名upload库来制作上传图片的功能。

一、安装第三方库

1.在composer.json文件中的”require”中加入

?

1
"codeguy/upload": "*"

Symfony2使用第三方库Upload制作图片上传实例详解

2.运行指令安装

?

1
composer update

二、编码

1.编写uploadpic方法上传图片,并将上传图片的用户id作为文件名

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58
<?php

/**

* @author sun

* by blogs.zmit.cn http://blogs.zmit.cn

* 原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处 http://blogs.zmit.cn/6544.html

* 中梦博客,作者信息和本声明。否则将追究法律责任。

*/

namespace zm\\adminbundle\\controller;

use symfony\\bundle\\frameworkbundle\\controller\\controller;

use symfony\\component\\filesystem\\filesystem;

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 \\upload\\storage\\filesystem($path);

$file = new \\upload\\file($str, $storage);

//如果文件名为空

if ($file->getname() != '') {

//设置文件名为用户的id

$file->setname($user_id);

//验证文件上传

$file->addvalidations(array(

//指定文件类型

new \\upload\\validation\\mimetype(array('image/png', 'image/jpg', 'image/jpeg', 'image/gif')),

//指定文件大小

new \\upload\\validation\\size('2m')

));

//上传文件

try {

//成功

$file->upload();

//文件名和扩展名

$file_name = $file->getnamewithextension();

} catch (\\exception $e) {

//失败!

$errors = $file->geterrors();

}

}

//返回文件名和扩展名

return $file_name;

}

}

2.用户上传头像,并将头像全路径存入数据库表

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48
<?php

/**

* 联系人控制器

* @author sun

* by blogs.zmit.cn http://blogs.zmit.cn

* 原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处 http://blogs.zmit.cn/6544.html

* 中梦博客,作者信息和本声明。否则将追究法律责任。

*/

namespace zm\\apibundle\\controller;

//引用写好的上传图片方法uploadpic的controller,并命名为basecontroller

use zm\\adminbundle\\controller\\defaultcontroller as basecontroller;

use symfony\\component\\httpfoundation\\request;

use symfony\\component\\httpfoundation\\response;

//继承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作为文件名,并修改表字段值为图片的全路径

Symfony2使用第三方库Upload制作图片上传实例详解

本文永久地址:http://blog.it985.com/6544.html
本文出自 it985博客 ,转载时请注明出处及相应链接。

希望本文所述对大家基于symfony框架的php程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Symfony2使用第三方库Upload制作图片上传实例详解 https://www.kuaiidc.com/99215.html

相关文章

发表评论
暂无评论