Upload files in PHP. MVC

Source: Internet
Author: User

PHP. the official website of MVC introduces the use of the pear: http_upload package to process uploads, while I only use the original method to process uploads, the main purpose is to demonstrate how to upload files in the MVC framework. in the future, I may use the pear: http_upload package.

1. Open the phpmvc-config.xml and add it to the

Configuration location. Of course, we need to save these files to our own directory. tempdir is the directory.
2. Save the file. Now write a PHP page with a form for uploading the file:
<Form enctype = 'multipart/form-data' method = 'post' action = 'main. php? Do = upload'>
<Input type = 'file' name = 'file'> <br/>
<Input name = 'submit _ files' value = 'upload' type = 'submit '>
</Form>
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

In the validate method, you must first obtain the values in the XML configuration file:
$ Appconfig = & $ this-> actionserver-> getapplicationconfig ($ request );
$ Controllerconfig = & $ appconfig-> getcontrollerconfig ();
$ Tmpfiledir = $ controllerconfig-> gettempdir ();
$ Maxfilesize = intval ($ controllerconfig-> getmaxfilesize (), 10 );
You must use the intval function to convert the file size into integer data.
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, and name and size indicate the temporary file address (including the file name) of the file. The source file

Name and size. The same size also needs to be converted to an integer
The following code checks whether the uploaded file meets our size requirements:
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 common action. It can be used to process uploaded files or directly redirect files.

To the successfully uploaded page.
5. Configure your actionform and action to the phpmvc-config.xml. Run it!

Related Article

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.