当前位置:Linux教程 - Php - php处理http上传文件函数转载

php处理http上传文件函数转载

PHP处理HTTP上传文件函数(转载).  

出处:我爱PHP    作者: admin   阅读次数:22

版权申明:本站发布的原创文章或作品版权归我站和作者共有,如需转载,请注明出处和作者。本站所提供的插画,摄影作品,以及设计作品,如需使用,请与原作者联系,版权归原作者所有。




Arvan写的上传函数


<?php
/**
+--------------------------------------------------
|   Function:_asUpFiles
|   Author:   Arvan [2fs.cn]
|   Action:  处理文件上传
|   Parameter:
|   @param: $dir      -- 存放目录,最后不带"/" [字串]
|   @param: $file_var -- 表单变量 [字串]
|   @param: $max_size -- 设定最大上传值,以k为单位. [整数/浮点数]
|   @param: $type     -- 限定后辍名(小写),多个用"/"隔开,不限定则留空 [字串]
|   @param: $name     -- 上传后命名,留空则为原名,true为系统随机定名 [布林值]
|   @return:
|       上传成功的$dir之后文件名
|   @e.g. :
|                _asUpFiles("file", "file", '', '', true);
+--------------------------------------------------
*/

function _asUpFiles($dir, $file_var, $max_size='', $type='', $name=false)
{
        if (!
is_writeable($dir))
        {
                echo
$dir." Can't be written or doesn't exist.";
                exit();
        }

        
$upfile     =& $_FILES["$file_var"];
        
$upfilename =  $upfile['name'];
        if (!(
$upfilename===''))
        {
                if (!
is_uploaded_file($upfile['tmp_name']))
                {
                        echo
$upfilename." Can't be uploaded.";
                        exit();
                }

                if (
$max_size>0 && $upfile['size']/1024>$max_size)
                {
                        echo
'The size of the file exceeds the limitative value.';
                        exit();
                }

                
$ext_name = strtolower(str_replace(".", "", strrchr($upfilename, ".")));
                if (!(
$type==='') && strpos($type, $ext_name)===false)
                {
                        echo
"Please upload the file of $type form.";
                        exit();
                }

                
$uploadname = $name ? md5(uniqid(rand())).".".$ext_name : $upfilename;
                if (!
move_uploaded_file($upfile['tmp_name'], $dir."/".$uploadname))
                {
                        echo
"Possible file upload attack! Here's some debugging info:\n";
                        
print_r($_FILES);
                        exit();
                }

                return
$uploadname;
        }
        else
        {
                return
'';
        }
}
?>