PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解,和大家讨论讨论,对PHP上传文件支持也越来越完美,在PHP 4.0.3以前我们只能用copy() 和 is_uploaded_file()方法结合来上传文件,呵呵,其实就是这样我们也会感觉到PHP相对于ASP来说上传文件要方便很多,这也就是本人选择PHP的一点小小的引以为豪的地方。
PHP上传文件例:
<ol class="dp-xml"> <li class="alt"><span><span>if (!copy($file, $file.’.bak’)) { </span></span></li> <li class=""> <span>print (”failed to copy $file…</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>n”); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>is_uploaded_file(string filename) </span></li> </ol>
如果 filename 所给出的文件是通过 HTTP POST 上传的则返回 TRUE。
<ol class="dp-xml"><li class="alt"><span><span>move_uploaded_file(string filename, string destination) </span></span></li></ol>
本函数检查并确保由 filename 指定的文件是合法的PHP上传文件(即通过 PHP 的 HTTP POST 上传机制所上传的)。如果文件合法,则将其移动为由 destination 指定的文件。由三个函数的解释就可以得到一个结论,move_uploaded_file()是copy()和is_uploaded_file()函数的结合,讲了这么半天大家用起这三个函数可能还会有些生疏,下面给大家两个例子:首先,建立一个前台页面,命名为index.html。
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">form</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">enctype</font></span><span>=”multipart/form-data” </span><span class="attribute"><font color="#ff0000">action</font></span><span>=”_URL_” </span><span class="attribute"><font color="#ff0000">method</font></span><span>=”post”</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span></span></li> <li class=""> <span>Send this file: </span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=”filename” </span><span class="attribute"><font color="#ff0000">type</font></span><span>=”file”</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">type</font></span><span>=”submit” </span><span class="attribute"><font color="#ff0000">value</font></span><span>=”Send File”</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">form</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
1,下面是关于copy()和is_uploaded_file()方法上传文件的方法(保存为:copy.php,上传文件需要将index.html中的_URL_改为copy.php)
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">path</font></span><span>=”./uploadfiles/”;//上传文件的路径 </span> </li> <li class="alt"><span>if(is_uploaded_file($filename)) </span></li> <li class=""><span>//特别注意这里,传递给is_uploaded_file的为$filename,可不要传递$_FILES[”filename”][”name”] </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">file1</font></span><span>=$_FILES[”filename”][”name”]; </span> </li> <li class="alt"><span>//注意这里$_POST[]方法为PHP4.1.0及以上版本支持,PHP以下的版本要用$HTTP_POST_FILES[]方法 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">file2</font></span><span>=$path.time().$file1; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">flag</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">1</font></span><span>; </span> </li> <li class=""><span>} </span></li> <li class="alt"> <span>if($flag) $</span><span class="attribute"><font color="#ff0000">result</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">copy</font></span><span>($file1,$file2); </span> </li> <li class=""><span>if($result) echo “上传成功!”; </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
2,下面是关于move_uploaded_file()方法上传文件的方法(保存为:move.php,上传文件需要将index.html中的_URL_改为move.php)
<ol class="dp-xml"> <li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li> <li class=""> <span>$</span><span class="attribute">path</span><span>=”./uploadfiles/”;//上传路径 </span> </li> <li class="alt"><span>if($_FILES[”filename”][”name”]) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">file1</span><span>=$_FILES[”filename”][”name”]; </span> </li> <li class=""> <span>$</span><span class="attribute">file2</span><span> = $path.time().$file1; </span> </li> <li class="alt"> <span>$</span><span class="attribute">flag</span><span>=</span><span class="attribute-value">1</span><span>; </span> </li> <li class=""><span>} </span></li> <li class="alt"> <span>if($flag) $</span><span class="attribute">result</span><span>=</span><span class="attribute-value">move_uploaded_file</span><span>($_FILES[”filename”][”tmp_name”],$file2); </span> </li> <li class=""><span>//特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件 </span></li> <li class="alt"><span>if($result) echo “上传成功!”; </span></li> <li class=""> <span></span><span class="tag">?></span><span> </span> </li> </ol>
文件上传就讲到这里了,这只是两个小程序,其实要完成文件上传所要的代码可不是这些就能了事的。一般还要有文件大小限制,文件扩展名选取,还有一些其它的安全方案。本人就写到这了,大家有兴趣写个完善的文件上传系统。呵呵!!!