Symfony2 uses a third-party library Upload to create image Upload instances ,. Symfony2 uses a third-party library Upload to create an image Upload instance. This article describes how Symfony2 uses a third-party library Upload to Upload images. For your reference, Symfony2 uses a third-party library Upload to create image Upload instances,
This document analyzes how Symfony2 uses a third-party library Upload to Upload images. We will share this with you for your reference. The details are as follows:
We generally have the Avatar setting function in personal data of applications or websites. in this chapter, we use a third-party Upload Library in Symfony2 to create and Upload images.
1. install third-party libraries
1. add "require" in the composer. json file
"codeguy/upload": "*"
2. run commands to install
composer update
II. encoding
1. Compile the uploadPic method to upload an image and use the user ID of the uploaded image as the file name.
<? Php/*** @ author Sun * By blogs.zmit.cn http://blogs.zmit.cn * Original works can be reproduced. during reprinting, you must mark the original source of the article in hyperlink form. http://blogs.zmit.cn /6544.html * The Author information and this statement of the Dream blog. Otherwise, legal liability will be held. */Namespace ZM \ AdminBundle \ Controller; use Symfony \ Bundle \ FrameworkBundle \ Controller; use Symfony \ Component \ Filesystem; class DefaultController extends Controller {public function indexAction ($ name) {return $ this-> render ('zmadminbundle: Default: index.html. twig ', array ('name' => $ name);}/*** Upload image ** @ param type $ user_id user id, used as the file name * @ param type $ str name of the file type input in the form * @ Param type $ path: save path * @ return type */public function uploadPic ($ user_id, $ str, $ path) {$ fs = new Filesystem (); // check whether the path exists if (! $ Fs-> exists ($ path) {// If not, create the directory $ fs-> mkdir ($ path, 0700 );} // use the Upload Library $ storage = new \ Upload \ Storage \ FileSystem ($ path); $ file = new \ Upload \ File ($ str, $ storage ); // if the file name is empty, if ($ file-> getName ()! = '') {// Set the file name to the user's id $ file-> setName ($ user_id ); // verify the file Upload $ file-> addValidations (array (// specify the file type new \ Upload \ Validation \ Mimetype (array ('image/png ', 'image/jpg ', 'image/jpeg ', 'image/GIF'), // specify the file Size new \ Upload \ Validation \ Size ('2 m '))); // upload the file try {// success $ file-> upload (); // file name and extension $ file_name = $ file-> getNameWithExtension ();} catch (\ Exception $ e) {// failed! $ Errors = $ file-> getErrors () ;}// return the file name and extension return $ file_name ;}}
2. the user uploads the avatar and saves it to the database table in the full path.
<? Php/*** contact controller * @ author Sun * By blogs.zmit.cn http://blogs.zmit.cn * Original works can be reproduced. during reprinting, you must mark the original source of the article in hyperlink form. http://blogs.zmit.cn /6544.html * The Author information and this statement of the Dream blog. Otherwise, legal liability will be held. */Namespace ZM \ ApiBundle \ Controller; // reference the Controller of the uploaded image method uploadPic and name it BaseControlleruse ZM \ AdminBundle \ Controller \ defacontroller Controller AS BaseController; use Symfony \ Component \ HttpFoundation \ Request; use Symfony \ Component \ HttpFoundation \ Response; // inherit BaseControllerclass ContactController extends BaseController {/*** Upload profile picture ** @ return Response */public function uploadHeadAction () {$ request = Req Uest: createFromGlobals ()-> request; $ user_id = $ request-> get ('User _ id '); // determine whether a file has been uploaded 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); // determines whether the user has if (! Empty ($ data ['id']) {// sets the image storage path $ path = 'image/head /'; // get the file name and extension $ file_name = $ this-> uploadPic ($ user_id, 'head', $ path) returned after the file is uploaded ); // modify the head avatar field of the user contact table $ conn-> executeUpdate ("UPDATE contact SET head =? WHERE id =? ", Array ($ path. $ file_name, $ user_id); $ result ['flag'] = 1; $ result ['content'] = 'The Avatar is uploaded successfully! ';} Else {$ result ['flag'] = 3; $ result ['content'] = 'User does not exist! ';}} Else {$ result ['flag'] = 2; $ result ['content'] =' Upload failed. no image is selected! ';} Return new Response (json_encode ($ result), '000000', array ('content-type' => 'application/json '));}}
In this way, the image is uploaded successfully, the user id is used as the file name, and the table field value is changed to the full path of the image.
Permanent Address: http://blog.it985.com/6544.html
This article is from the IT985 blog. please indicate the source and relevant links when reprinting.