<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function addUpload() {
var container = document.getElementById('myUploadContainer');
var num = document.getElementById('total');
var newNum = (num.value-1)+2;
num.value = newNum;
var uploadDiv = document.createElement('div');
var anotherBox = document.createElement('input');
var boxName = 'uploadBox_'+newNum;
anotherBox.setAttribute("type","file");
anotherBox.setAttribute("name",boxName);
uploadDiv.setAttribute("id",boxName);
uploadDiv.appendChild(anotherBox);
uploadDiv.innerHTML = uploadDiv.innerHTML+" <a href='#' onclick='removeField(\""+boxName+"\");'>[เอาออก]</a>";
container.appendChild(uploadDiv);
}
function removeField(fieldID){
var container = document.getElementById('myUploadContainer');
container.removeChild(document.getElementById(fieldID));
}
//-->
</script>
</head>
<body>
<form action="up.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="boxes" value="0" id="total" />
<div id="myUploadContainer"> </div>
<p><a href="javascript:;" onclick="addUpload();">แนบไฟล์</a></p>
<div><input type='submit' value="Upload" /></div>
</form>
</body>
</html>
up.php
<?php
$count = $_POST['boxes']; // get the total number of upload fields
$uploadPath = 'someFolder/someSubFolder/';//path to the folder you want to upload to
// a simple for() loop to repeat upload action how many times it is needed
for($c = 1; $c <= $count; $c++){
$file = $_FILES['uploadBox_'.$c]['tmp_name']; // get the file
//check if the upload field isn't empty
if(!empty($file)){
//validate copy - return success message if the file's uploaded or a fail message
//if not!
if(copy($file, $uploadPath.$_FILES['uploadBox_'.$c]['name'])){
echo $_FILES['uploadBox_'.$c]['name']." uploaded successfully<br />";
}else{
echo "failed to copy ".$_FILES['uploadBox_'.$c]['name']."...<br />";
}
}
}
?>
ผมลองเอาไปทดสอบแหล่ะ ให้มัน alert(uploadDiv.innerHTML) ปรากฎว่า
firefox เห็น type เห็น name
แต่ ie เห็น type แต่ไม่เห็น name
ผมเพิ่งรู้น่ะครับว่า มี attribute บางตัวมันไม่ support กับพวก IE ด้วย :<
เลยลองหาวิธีแก้เอามาฝากครับ Code
var uploadDiv = document.createElement('div');
var anotherBox = document.createElement('input');
var boxName = 'uploadBox_'+newNum;
anotherBox.setAttribute("type","file");
anotherBox.setAttribute("name",boxName);
แก้เป็น Code
var boxName = 'uploadBox_'+newNum;
try{
var anotherBox = document.createElement("<input type=\"file\" name=\""+boxName+"\" />");
}catch(e){
var anotherBox = document.createElement('input');
anotherBox.setAttribute("type","file");
anotherBox.setAttribute("name",boxName);
}
var uploadDiv = document.createElement('div');
Date :
2009-10-19 01:04:43
By :
xbeginner01
No. 5
Guest
ลอง เอา บรรทัดนี้ออกครับ หรือ comment ไว้
>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">