Zend framework 2 file upload example

- Now we will learn about how to upload files one file or multiple files at a time before we come to the next song on Add - Delete - Edit
- First, we will create a new name CONTROLLER /module/Admin/src/Admin/Controller/UploadController.php read as follows:




- When creating a new CONTROLLER we will have to declare it to the MODULE so we open the file /modules/admin/config/search module.config.php search code:


- Change to:

- Corresponding UploadController we will have a folder containing the CONTROLLER ACTION VIEW of this. We create a directory / folder upload / modules / admin / views / admin
- In the name ACTION indexAction UploadController there so we need to create a file for ACTION VIEW this. Create the file / modules / admin / views / admin / upload / index.phtml with the following contents:

- After we have created an ACTION to upload images. We open the file / public / css / style.css add CSS code to create a FORM

- Next we create the directory / form in / modules / admin / src / Admin. This folder will contain the FORM CLASS MODULE make our
- In the folder / modules / admin / src / Admin / Form 1 we create a new file named UploadForm.php and we put the following contents:

- After you have created UploadForm now our job to take ACTION FORM we pass on the VIEW.
- Open the file / modules / admin / src / admin / controller / UploadController.php add the following line to the file

- Next we will modify the contents of the method indexAction () as follows:

- After the transmission of values ​​from ACTION ngoaiVIEW we will print information to see
- Open the file / modules / admin / views / admin / upload / index.phtml edit the content as follows:

- If the interface that appears on this object UploadForm has proven to work. Now we will proceed with the transfer UploadForm objects into HTML code.
- Open the file / modules / admin / views / admin / upload / index.phtml edit the content as follows:

- We'll run the URL: localhost: 8000/zf2basic/public/admin/upload/index
- You see the error appears. The error appears like this is difficult because the fix before adding new code in place we have been UploadForm object without error at all. But after adding the error appears. Add our code should totally write correctly is definitely not caused by the above code.

- The object Zend / Form / Form link to an object that is Zend \ I18n \ Translator \ Translator to assist in language conversion on the application interface. So to fix this error we need to declare objects Zend \ I18n \ Translator \ Translator in module.config.php file of this module. We proceed as follows, open the file / modules / admin / config / module.config.php add a new element to the array configuration:

- Now all the file / modules / admin / config / module.config.php will be as follows:


- After adding the code to declare objects Zend \ I18n \ Translator \ Translator We try refreshing the URL localhost: 8000/zf2basic/public/admin/upload/index
- Now we create a new directory called / files in the folder / public to upload images to us
- Next we will declare a constant to store the path to the folder in the file upload files / public / define.php. Open the file / public / define.php add the following line:

- Now we revise the code indexAction () to upload the file to the folder / public / files

- We refresh URL: localhost: 8000/zf2basic/public/admin/upload/index. Click any file to upload, the next we click "Send data", then we try to check the folder / public / files are uploaded or not
Zend framework 2 file upload example
Zend framework 2 file upload example

1 comment:

  1. Hi, a problem to ask you ,that is I want to rename each file, when I uploading multiple files once. How can I get it.

    ReplyDelete