move_uploaded_file()和copy()实现文件上传的比较

下面是表单代码:

====================================================================

<html> 
<body> 
<title>文件上传</title> 
<form enctype="multipart/form-data" method="post" action="upload.php"> 
<input type=file name="upfile" size="10"><br><br> 
<input type=submit value='上载文件'> 
</form> 
</body> 
</html>

====================================================================

<?php
$uploaddir = "uploadfiles/";
$uploadfile = $uploaddir. $_FILES['upfile']['name'];
if (move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile)) {
echo "上传成功!";
else 
echo "上传失败!";
?>

====================================================================

下面是用copy()函数来实现文件上传的代码:
====================================================================

<?php
$uploaddir="uploadfiles/"
$uploadfile=$uploaddir.$_FILES['upfile']['name']; 
if (copy($_FILES['upfile']['tmp_name'],$uploadfile)) 
echo ("上传成功!"); 
else  
echo ("上传失败!"); 
?>

====================================================================

以上二例力求简单,具体在做时,要加入一些文件类型检验函数等。
需要注意一个重要的问题就是$uploaddir写的是相对于$_SERVER[‘PHP_SELF’]的一个相对路径,记好,前面不要加”/”后面要加”/”。

move_uploaded_file()比copy()要快些

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注