Upload files in PHP. MVC

Source: Internet
Author: User
PHP. on the official website of MVC, the application PEAR: HTTP_Upload is used to process the upload. However, I only use the original method to process the upload, it is important to clarify how to upload files in the MVC framework. in the future, I may apply the PEAR: HTTP_Upload package

PHP. on the official website of MVC, the application PEAR: HTTP_Upload is used to process the upload. However, I only use the original method to process the upload, it is important to clarify how to upload files in the MVC framework. in the future, I may apply the PEAR: HTTP_Upload package.

1. open the phpmvc-config.xml, in Add the following to the tag:


MaxFileSize is the maximum size of the uploaded file, measured in bytes. the uploaded file is stored in php with a temporary name. ini configuration status, of course we need to save these files to our own directory, tempDir is this directory.

2. keep the file. now write a php page with a form for uploading the file:

The enctype must be set to multipart/form-data.

3. write an ActionForm class, such as UploadForm. this class is similar to ordinary ActionForm, but we need to implement the upload control in the validate method. first, you must obtain the values in the xml configuration file:

$ AppConfig = & $ this-> actionServer-> getApplicationConfig ($ request );
$ ControllerConfig = & $ appConfig-> getControllerConfig ();
$ TmpFileDir = $ controllerConfig-> getTempDir ();
$ MaxFileSize = intval ($ controllerConfig-> getMaxFileSize (), 10 );

The file size must be converted into integer data using the intval function.

Then we get the information of the uploaded file:

$ Upload_file = $ _ FILES ['file'] ['tmp _ name'];
$ Upload_file_name = $ _ FILES ['file'] ['name'];
$ Upload_file_size = intval ($ _ FILES ['file'] ['size'], 10 );

['File'] indicates the name of the upload form. tmp_name. the name and size indicate the temporary file address (including the file name) of the file, and the source file name and size. The size also needs to be converted to an integer.

The following is a request to determine whether the uploaded file has reached our size:

If ($ upload_file_size> $ maxFileSize)
{
// Handle errors here
}

Finally, copy our file to the target Directory:

If (! Copy ($ upload_file, $ tmpFileDir. $ upload_file_name ))
{
// Copy failed here
}

4. The following is the Action Class. this class is the same as a normal Action. it can be used to process uploaded files, or directly redirected to a successfully uploaded page.

5. configure your ActionForm and Action to the phpmvc-config.xml. run it!

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.