Uploading files in Php.mvc

Source: Internet
Author: User

Php. The MVC official website describes the use of pear::http_upload packages to process uploads, and I just upload them in the original way, mainly to illustrate how to upload files in the framework of MVC. I might use a Pear::http_upload package later.

1. Open Phpmvc-config.xml and add in

<controller maxfilesize= "1048576" tempdir= "C:\www\upload\" >
</controller>

Where MaxFileSize is the maximum size of the upload file, the unit is byte, the uploaded file will be stored in a temporary name in php.ini configured location, we will of course save these files to our own directory, TempDir is this directory.

2. Save the file. Now write a PHP page, which writes a form for uploading files:

<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>

Where enctype must be set to Multipart/form-data

3. Write a Actionform class, such as Uploadform, this class is similar to the ordinary actionform, but we want to implement the operation of the uploaded file in the Validate method. First you get the values in the XML configuration file:

$appConfig =& $this->actionserver->getapplicationconfig ($request);
$controllerConfig =& $appConfig->getcontrollerconfig ();
$tmpFileDir = $controllerConfig->gettempdir ();
$maxFileSize = Intval ($controllerConfig->getmaxfilesize (), 10);

Be sure to convert the file size to the Intval function into the shaping data.

Then get the file information we uploaded:

$upload _file=$_files[' file ' [' Tmp_name '];
$upload _file_name=$_files[' file ' [' Name '];
$upload _file_size=intval ($_files[' file '] [' size '],10];

[' file '] is the name of the upload form. Tmp_name,name and size are the temporary file addresses (including file names) of this file, the source file name, and the size. The same size needs to be converted to plastic

The following determines whether the uploaded files meet our size requirements:

if ($upload _file_size > $maxFileSize)
{
Here to do the error handling
}

Finally copy our files to the destination directory:

if (!copy ($upload _file, $tmpFileDir. $upload _file_name))
{
Here do replication failure processing
}

4. The following is the action class, this class and ordinary action like, inside can do some file upload processing, can also directly redirect to upload the successful page.

5. Put your actionform and action configuration into the Phpmvc-config.xml, run a try!



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.