河汉清且浅,牵牛敦而纯

php上传文件简单示例

 

<?php

$log = '';
$buf = ob_get_contents();
ob_end_clean();
$postmaxsize = trim(ini_get('post_max_size'));
//preg_match_all('/\d+/', $postmaxsize ,$arr);
//$number = $arr[0][0];
//if(strpos($postmaxsize, 'M') !== false) $postmaxsize = $number*1024*1024;
//if(strpos($postmaxsize, 'G') !== false) $postmaxsize = $number*1024*1024*1024;

if(strpos($buf, 'POST Content-Length') !== false) $log .= "上传文件大于最大值 $postmaxsize ";
else $log .= $buf;

if(isset($_POST['uploadaction'])){
    set_time_limit(60);
    $_POST['uploadaction'] = 0;
    $filepath = "d:/htdocs/test/"; # 上载文件存放路径

    $upfile_name = $_FILES['upfile']['name'];
	$upfile_size = $_FILES['upfile']['size'];
	$upfile_type = $_FILES['upfile']['type'];
	$upfile_tmp  = $_FILES['upfile']['tmp_name'];
	$upfile_error= $_FILES['upfile']['error'];

    if($upfile_size < 1024) $filesize = $upfile_size . " Byte";
    elseif($upfile_size < (1024 * 1024)) $filesize = number_format((double)($upfile_size / 1024), 1) . " KB";
    else $filesize = number_format((double)($upfile_size / (1024 * 1024)), 1) . " MB";

    if(($upfile_tmp != "none") && ($upfile_tmp != "")){
        $filename = $filepath . $upfile_name;
        if(!file_exists($filename)){
            if(copy($upfile_tmp, $filename)){
                unlink($upfile_tmp);
                $log .= " $upfile_name [ $filesize ]已上传至 $filepath ";
            }else $log .= " $upfile_name 上载失败!";
		}else $log .= " $upfile_name 已经存在!";
    }

    set_time_limit(30); //恢复默认超时设置
}
?>
<html>
<head>
  <title>文件上传</title>
</head>
<body>
  <center>
  <h2>文件上传</h2>
  <form
    action="<?php echo $_SERVER['PHP_SELF']; ?>"
    method="post"
    enctype="multipart/form-data"
  >
    <input type="hidden" name="max_file_size" value="8388608" />
    <input type="hidden" name="uploadaction" value="1" />
    <input type="file" name="upfile" size="30" />
    <input name="submit" value="提交" type="submit" />
    <input name="reset" value="重置" type="reset" />
  </form>
  <?php echo "<br>". $log ."\r\n"; ?>
  </center>
</body>
</html>

  

posted on 2023-04-06 14:39  伊索  阅读(18)  评论(0编辑  收藏  举报