当前位置:主页 >> PHP >> 正文
php实现一次性多张图片上传功能
阅读:62 输入:2020-02-11 20:27:56

1.index.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>上传文件</title></head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input name='uploads[]' type="file" multiple />
<input type="submit" name="uploadpic" value="上传" />
</form>
</body>
</html>

2.upload.php

<?php
$uploadfile;//图片名字
if($_POST["uploadpic"]=="上传"){
	$dest_folder="picture/";//上传图片保存的路径 图片放在跟你upload.php同级的picture文件夹里
	$arr=array();//定义一个数组存放上传图片的名称
	if(!file_exists($dest_folder)){
		mkdir($dest_folder,700); // 创建文件夹,并给予最高权限
	}//if file_exists
	$tp = array("image/gif","image/pjpeg","image/jpeg","image/png"); 
	foreach ($_FILES["uploads"]["error"] as $key => $error){
		 if(!in_array($_FILES["uploads"]["type"][$key],$tp)){
			echo "<script language='javascript'>";
			echo "alert(\"文件类型错误!\");";
			echo "</script>";
			exit;
		 }//ifin_array
	if($error == "UPLOAD_ERR_OK"){
		$tmp_name = $_FILES["uploads"]["tmp_name"][$key];
		$a=explode(".",$_FILES["uploads"]["name"][$key]);//图片名扩展名
		// $prename = substr($a[0],10);
		$prename = $a[0];
		$name = date('YmdHis').mt_rand(100,999).".".$a[1];// 文件的重命名 (日期+随机数+后缀)
		$uploadfile = $dest_folder.$name;// 文件的路径
		move_uploaded_file($tmp_name, $uploadfile);
		
		 // 插入到数据库代码
}//if error

	}//foreach
	}//if POST