单文件上传 Link to heading

<?php
header("Content-type:text/html;charset=utf-8");

/*
* @param1 array  $file , 上传文件的信息
* @param2 array  $allow_type , 运行上传的mime
* @param3 string $path , 存储的路径
* @param4 string &$error , 错误消息
* @param5 array  $allow_format , 允许上传的文件格式
* @param6 int    $max_size , 允许上传的最大值(必须比配置文件中的值小)
*/
function upload_single($file,$allow_type,$path,&$error,$allow_format = array(),$max_size = 2000000){
  // 判断文件是否有效
  if(!is_array($file) || !isset($file['error'])){
    $error = '不是一个有效的文件';
    return false;
  }
  // 判断文件上传路径是否存在
  if(!is_dir($path)){
    $error = '文件存储路径不存在';
    return false;
  }
  // 判断文件上传过程是否出错
  switch ($file['error']) {
    case 1:
    case 2:
      $error = '文件超出预设值';
      return false;
    case 3:
      $error = '文件上传过程出错';
      return false;
    case 4:
      $error = '没有选中要上传的文件';
      return false;
    case 6:
    case 7:
      $error = '文件保存失败';
      return false;
  }
  // 判断mime类型
  if(!in_array($file['type'],$allow_type)){
    $error = '请上传正确的文件类型';
    return false;
  }
  // 判断文件后缀
  $ext = ltrim(strrchr($file['name'],'.'),'.');
  if(!empty($allow_format) && !in_array($ext,$allow_format)){
    $error = '请上传正确的文件类型';
    return false;
  }
  // 判断文件大小
  if($file['size'] > $max_size){
    $error = '当前文件超出大小,请不要大于' . $max_size . '字节';
  }
  // 移动到指定目录
  if(!is_uploaded_file($file['tmp_name'])){
    $error = '错误,不是上传文件';
    return false;
  }
  // 构造文件名字并上传
  $fullname = strstr($file['type'],'/',true) . date('Ymd') . mt_rand(10000,99999) . '.' .$ext;
  if(move_uploaded_file($file['tmp_name'],$path . '/' . $fullname)){
    return $fullname;
  }else{
    $error = '文件上传失败';
    return false;
  }
}

$file = $_FILES['img'];
$path = 'upload';
$allow_type = array('image/jpg','image/jpeg');
$allow_format = array('jpg','jpeg','JPG','JPEG');
$max_size = 1048576;

if($filename = upload_single($file,$allow_type,$path,$error,$allow_format,$max_size)){
  echo $filename;
}else{
  echo $error;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>单上传文件</title>
</head>
<body>
  <div>
    <form action="2.4.php" method="post" enctype="multipart/form-data"> 
      <input type="file" name="img" />
      <input type="submit" />
    </form>
  </div>
</body>
</html>

多文件上传 Link to heading

<?php
header("Content-type:text/html;charset=utf-8");
// 多文件一起上传
$fileName = $_FILES['img']['name'];
$fileTmp = $_FILES['img']['tmp_name'];
foreach($fileTmp as $key => $value){
  if(is_uploaded_file($value)){
    move_uploaded_file($value,'upload/' . $fileName[$key]);
    echo '文件上传成功';
  }else{
    echo '文件上传失败';
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
    <form action="2.2.php" method="post" enctype="multipart/form-data">
        <input type="file" name="img[]" multiple="multiple" />
        <input type="submit" value="上传" />
    </form>
</body>
</html>