欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-28 00:02:40
本文实例分析了symfony2使用第三方库upload制作图片上传的方法。分享给大家供大家参考,具体如下: 我们在应用程序或者网站的个人资料里一般都有设置头像的功能,这一...

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

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

一、安装第三方库

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

"codeguy/upload": "*"

Symfony2使用第三方库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 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.用户上传头像,并将头像全路径存入数据库表

<?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博客 ,转载时请注明出处及相应链接。

更多关于php框架相关内容感兴趣的读者可查看本站专题:《php优秀开发框架总结》,《codeigniter入门教程》,《ci(codeigniter)框架进阶教程》,《yii框架入门及常用技巧总结》及《thinkphp入门教程

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