PHP.MVC官方网站上介绍的是使用PEAR::HTTP_Upload 包来处理上传,而我只是用原始的方式来进行上传的处理,主要是为了说明在MVC的框架中怎样上传文件.以后我可能会使用PEAR::HTTP_Upload 包来做.
1.打开phpmvc-config.xml,在
其中maxFileSize为上传文件大小的最大值,单位是字节,上传的文件会用一个临时的名字存放在php.ini所配置的位置,我们当然要把这些文件另存到我们自己的目录中,tempDir就是这个目录.
2.保存文件.现在写一个php页,里面写一个上传文件的表单:
其中enctype一定要设定为multipart/form-data
3.写一个ActionForm类,比如叫UploadForm,这个类和普通的ActionForm大同小异,但我们要在其中的validate方法中实现操作上传的文件.首先要得到xml配置文件中的值:
$appConfig =& $this->actionServer->getApplicationConfig($request);
$controllerConfig =& $appConfig->getControllerConfig();
$tmpFileDir = $controllerConfig->getTempDir();
$maxFileSize= intval($controllerConfig->getMaxFileSize(),10);
一定要把文件大小那个用intval函数转换成整形数据.
接着得到我们上传的文件信息:
$upload_file=$_FILES['file']['tmp_name'];
$upload_file_name=$_FILES['file']['name'];
$upload_file_size=intval($_FILES['file']['size'],10);
['file']是上传表单的名字.tmp_name,name和size分别是这个文件的临时文件地址(包括文件名),源文件名和大小.同样大小也需要转换为整形
下面判断上传的文件是否达到我们的大小要求:
if($upload_file_size > $maxFileSize)
{
//这里做错误处理
}
最后拷贝我们的文件到目的目录:
if (!copy($upload_file,$tmpFileDir.$upload_file_name))
{
//这里做复制失败处理
}
4.下面是Action类,这个类就和普通的Action一样,里面可以做一些文件上传后的处理,也可以直接重定向到上传成功的页面.
5.将你的ActionForm和Action配置到phpmvc-config.xml中去,运行一下试试吧!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
