私はこれをほぼ2日間試しましたが、うまくいきません。単純な入力ファイル(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
}
}
}
しかし、それは1つのファイルしか受け取ることができません。コピーも使用しようとしましたが、同じ結果が得られました.countメソッド、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 ) ) )