首先,我很抱歉我的英语不好。
我想使用 HTML5 + JavaScript + PHP 将多个文件上传到服务器
用 HTML 和 JS 做就好像模拟做一样。正确操作。
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
.thumb {
height: 75px;
border: 1px solid #000;
margin: 10px 5px 0 0;
}
</style>
</head>
<body>
<form action="lanzar.php" enctype="multipart/form-data" method="post" >
<input type="file" id="files" name="files[]" multiple />
<input type="submit" />
<input type="reset"/>
<output id="list"></output>
</form>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</body>
</html>
然后我有一个简单的 PHP 代码可以显示图像。我的问题是只到最后一个被选中的文件。案子没有休息。就好像数组不是制作数据的正确方法。
if(isset($_FILES['files']['tmp_name'])){
echo $num_files = count($_FILES['files']['tmp_name']);
foreach($_FILES['files']['name'] as $key =>$value){
is_uploaded_file($_FILES['files']['tmp_name'][$key]);
$origen = $_FILES['files']['tmp_name'][$key];
$destino = "img/".$_FILES['files']['name'][$key];
move_uploaded_file($origen, $destino);
}
}
我在做什么坏事?谢谢