Symfony2 use third-party library upload to make picture upload instance detailed,
This paper analyzes the method of Symfony2 using third-party library upload to make image uploading. Share to everyone for your reference, as follows:
We generally have the function of setting the avatar in the profile of the application or the website, this chapter we use a third party's more famous upload library to make the function of uploading the picture in Symfony2.
First, install a third-party library
1. In "require" in the Composer.json file, add
"Codeguy/upload": "*"
2. Run the command installation
Composer Update
Second, the Code
1. Write the Uploadpic method to upload the image, and upload the image as the user ID as the file name
<?php/** * @author Sun * by blogs.zmit.cn http://blogs.zmit.cn * Original works, permission to reprint, please be sure to use hyperlinks in the form of the original source of the article http://blogs.zmit.cn/ 6544.html * Chinese Dream Blog, author information and this statement. Otherwise, the legal liability will be investigated. */namespace Zm\adminbundle\controller;use Symfony\bundle\frameworkbundle\controller\controller;use Symfony\ Component\filesystem\filesystem;class Defaultcontroller extends Controller {public Function indexaction ($name) {Retu RN $this->render (' ZMAdminBundle:Default:index.html.twig ', Array (' name ' = = $name)); /** * Upload image * * @param type $user The ID of the _id user, use as file name * @param type $str name of input of type file in form * @param type $path Save path * @return Type */Public function uploadpic ($user _id, $str, $path) {$fs = new Filesystem (); Check if the path exists if (! $fs->exists ($path)) {//If not present, create directory $fs->mkdir ($path, 0700); }//using Upload library $storage = new \upload\storage\filesystem ($path); $file = new \upload\file ($STR, $storage); If the file name is null if ($file->getname ()! = ") {//sets the file name to the user's ID $file->setname ($user _id); Verify File Upload $file->addvalidations (Array (//Specify file Type new \upload\validation\mimetype (Array (' image/png ', ' I Mage/jpg ', ' image/jpeg ', ' image/gif '),//Specify File Size new \upload\validation\size (' 2M ')); Upload file try {//Success $file->upload (); File name and extension $file _name = $file->getnamewithextension (); } catch (\exception $e) {//failed! $errors = $file->geterrors (); }}//Return file name and extension return $file _name; }}
2. The user uploads the avatar and stores the full path of the avatar into the database table
<?php/** * Contact Controller * @author Sun * by blogs.zmit.cn http://blogs.zmit.cn * Original works, permission to reprint, please be sure to use hyperlinks in the form of the original source of the article http://blogs.z mit.cn/6544.html * Chinese Dream Blog, author information and this statement. Otherwise, the legal liability will be investigated. */namespace zm\apibundle\controller;//refer to the Uploadpic Controller that wrote the upload image method and named Basecontrolleruse zm\adminbundle\ Controller\defaultcontroller as Basecontroller;use symfony\component\httpfoundation\request;use Symfony\Component\ httpfoundation\response;//inherit Basecontrollerclass Contactcontroller extends Basecontroller {/** * user uploads avatar * * @return Response */Public Function uploadheadaction () {$request = Request::createfromglobals ()->request; $user _id = $request->get (' user_id '); Determine if there is a file upload if (isset ($_files[' head ')) && $_files[' head ']! = ") {$conn = $this->getdoctrine ()->get Connection (); $data = $conn->FETCHASSOC ("Select ID, head from the contact WHERE id =?") LIMIT 1 ", Array ($user _id)); Determine if the user exists if (!empty ($data [' ID ')]) {//Set picture save path $path = ' image/head/'; Gets the file name and extension $file _name = $this->uploadpic ($user _id, ' head ', $path) returned after uploading the files; Modify the value of the user Contact table Head picture field $conn->executeupdate ("UPDATE contact SET head =?") WHERE id =? ", Array ($path. $file _name, $user _id)); $result [' flag '] = 1; $result [' content '] = ' upload avatar successfully! '; } else {$result [' flag '] = 3; $result [' content '] = ' user does not exist! '; }}else{$result [' flag '] = 2; $result [' content '] = ' upload failed, no picture Selected! '; } return new Response (Json_encode ($result), ' n ', Array (' content-type ' = ' Application/json ')); }}
So the picture is uploaded successfully, the user's ID as the file name, and modify the table field value as the full path of the picture
Permanent address of this article: http://blog.it985.com/6544.html
This article is from IT985 blog, please indicate the source and corresponding link when reproduced.
Read more about PHP framework related content Readers can view this site: "PHP Excellent Development Framework Summary", "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "Yii framework Introduction and common skills Summary" and " thinkphp Getting Started Tutorial "
It is hoped that this article is helpful to the PHP program design based on Symfony framework.
Articles you may be interested in:
- Symfony2 implementing the method of data built into the doctrine
- Symfony2 installing a third-party bundles instance
- The configuration method of Symfony2 under Nginx and graphic tutorial
- Symfony2 Installation Method (2 methods)
- Symfony2 Session Usage Example analysis
- High performance PHP Framework Symfony2 Classic Introductory Tutorial
- Symfony Learning 10-minute Introductory classic tutorial
- An example analysis of Symfony data verification method
- Symfony form and page implementation tips
- An example analysis of controller usage of SYMFONY2 development
http://www.bkjia.com/PHPjc/1098963.html www.bkjia.com true http://www.bkjia.com/PHPjc/1098963.html techarticle Symfony2 using third-party library upload to make picture upload instance, this paper analyzes Symfony2 method of using third-party library upload to make image uploading. Share to everyone for your reference, ...