How to use a third-party library Upload to create an image Upload instance in Symfony2

Source: Internet
Author: User
This article mainly introduces how Symfony2 uses a third-party library Upload to Upload images, and analyzes in detail the steps and skills of Symfony2 using a third-party library Upload based on the instance form, for more information about how to use the third-party library Upload to Upload images, see the example in this article. 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.

 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.

 Request; $ user_id = $ request-> get ('User _ id'); // determine whether a file is uploaded if (isset (

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.

  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.

___FCKpd___3

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.