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!