【发布时间】:2013-01-09 03:05:23
【问题描述】:
我已经尝试了将近两天,但它不起作用,我有一个简单的输入文件(在 php 中)
echo '<input id="files" type="file" name="ufile[]" multiple="multiple"/>';
假设同时发送多个文件到另一个页面,我接收文件的代码如下:
for($i=0; $i<count($_FILES['ufile']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['ufile']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = 'upload/'. $_FILES['ufile']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
但它只能接收一个文件,我也尝试使用复制,它给出了相同的结果,我尝试使用计数方法 print_r 和 var_dump 来计算文件数:
$a=count($_FILES['ufile']['name']);
print_r($a);
var_dump($a);
而且它们也都只显示文件。关于浏览器兼容性,我已经在少数浏览器上尝试过,包括最新版本的 Firefox、chrome 和 ...
提前致谢 我编辑这篇文章,包括输出 (var_dump) 并添加 html 表单 下面是 javascript 代码,在其中预览我使用输入文件选择的图像。 window.onload = function(){
if(window.File && window.FileList && window.FileReader)
{
var filesInput = document.getElementById("files");
filesInput.addEventListener("change", function(event){
var files = event.target.files; //FileList object
var output = document.getElementById("result");
for(var i = 0; i< files.length; i++)
{
var file = files[i];
if(!file.type.match('image'))
continue;
var picReader = new FileReader();
picReader.addEventListener("load",function(event){
var picFile = event.target;
var div = document.createElement("div");
div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
"title='" + picFile.name + "'/>";
output.insertBefore(div,null);
});
picReader.readAsDataURL(file);
}
});
}
else
{
console.log("not supported");
}
}
</script>
这是我的 html 表单:
echo '<form action="Data.php" method="post" enctype="multipart/form-data" name="form1" id="form1">';
echo'
<label for="files">Add image: </label>';
echo '<input id="files" type="file" name="ufile[]" multiple/>';
echo '
<output id="result" />';
echo '<input type="submit" name="submit" value="Submit"id="newbutton" style="width:100px;height:30px" />';
</form>
最后是 vardump 的结果
Array ( [ufile] => Array ( [name] => Array ( [0] => Awesoem-Arrow-Facebook-Timeline-Cover_02-@-GenCept.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C:\xampp\tmp\php6BDF.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 120664 ) ) )
【问题讨论】:
-
你能显示你最后一段代码的输出吗?
print_r($a)等 -
您能显示您的表单的完整 HTML 吗?
-
我也想看看 print_r($_FILES); 的输出当您尝试上传一个或多个文件时。
-
Array ([ufile] => Array ([name] => Array ([0] => Awesoem-Arrow-Facebook-Timeline-Cover_02-@-GenCept.jpg) [type] =>数组 ( [0] => image/jpeg ) [tmp_name] => 数组 ( [0] => C:\xampp\tmp\php6BDF.tmp ) [错误] => 数组 ( [0] => 0 ) [大小] => 数组 ( [0] => 120664 ) ) )
-
@DeeDee@Nicarus,我已经更新了我的帖子并添加了你所要求的一切!