php多文件上传实现代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php多文件上传实现代码相关的知识,希望对你有一定的参考价值。
参考技术A 这篇文章主要介绍了php多文件上传实现代码,需要的朋友可以参考下index_uploads.php
代码如下:
<html>
<head>
<meta
charset="utf-8">
<title>index_uploads</title>
</head>
<body>
<form
action="uploads.php"
method="post"
enctype="multipart/form-data">
<input
type="file"
name="file[]">
<br>
<input
type="file"
name="file[]">
<br>
<input
type="submit"
value="uploads">
</form>
</body>
</html>
uploads.php
代码如下:
<?php
header("content-type:text/html;charset=utf-8");
echo
"<pre>";
print_r($_FILES);
echo
"</pre>";
$count
=
count($_FILES['file']['name']);
for
($i
=
0;
$i
<
$count;
$i++)
$tmpfile
=
$_FILES['file']['tmp_name'][$i];
$filefix
=
array_pop(explode(".",
$_FILES['file']['name'][$i]));
$dstfile
=
"uploads/files/".time()."_".mt_rand().".".$filefix;
if
(move_uploaded_file($tmpfile,
$dstfile))
echo
"<script>alert('succeed!');window.location.href='index_uploads.php';</script>";
else
echo
"<script>alert('fail!');window.location.href='index_uploads.php';</script>";
核心:<1>上传首页中input的name属性是这么设置的。
<2>用while循环上传多文件。
ajax利用FormData实现多文件上传php获取
- 前台代码(注意,不需要用到form标签):
a. html部分:
b. js部分:
c. 完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form>
<input type="file" multiple id="lee_file">
<input type="text" id="lee_text" value="test">
<input type="button" value="上传图片" id="lee_button">
</form>
<script type="text/javascript" src="jquery.min.js"></script>
<script>
var lee_button = $(‘#lee_button‘);
function sendFile(){
var lee_file = $(‘#lee_file‘);
var lee_text = $(‘#lee_text‘);
var form_data = new FormData();
for(var i in lee_file[0].files){
form_data.append(‘pics[]‘,lee_file[0].files[i]);
}
form_data.append(‘text‘,lee_text.val());
$.ajax({
url: ‘http://localhost/111.php‘,
type: ‘POST‘,
cache: false,
data: form_data,
processData: false,
contentType: false
}).done(function(res) {
console.log(res);
}).fail(function(res) {
console.log(‘fail‘);
});
}
lee_button.click(function(){
sendFile();
})
</script>
</body>
</html> - php获取:
完整代码:
<?php
header(‘Access-Control-Allow-Origin: *‘);
$pics = $_FILES[‘pics‘];
var_dump($pics);
$text = $_POST[‘text‘];
var_dump($text);
以上是关于php多文件上传实现代码的主要内容,如果未能解决你的问题,请参考以下文章