JAVASCRIPT动态添加表单,多个文件上传问题

spotatoes 2008-12-06 10:31:55
问题是用IE浏览器,上传,始终最后一个文件没法上传上成功,获取的TMP_NAME,NAME都为空,用Firefox更坏,始终只能上传一个文件,其它所有添加的文件都上传不了,郁闷


if($action =='up'){
$vid = $_POST['S'];
$vid = intval($vid);
$rel = $MySelf->GetResult($SourceDBN,'Select * from ISource where IID='.$vid);
if( !$rel )
{
echo '<div class="errinfo">No Result at ID='.$vid.'</div>';
}else{
$vv = mysql_fetch_array($rel,MYSQL_BOTH);
if( !$vv ){ echo '<div class="errinfo">No Result at ID='.$vid.'</div>'; }
else{
$upc = intval($_POST['upcount']);

echo '<div class="errinfo">';
echo 'Upload count:'.$upc.'<br/>';
if( $upc>0 ){
$allok = true;
$js = intval($vv['IContent']);
for($v = 1; $v<=$upc; $v++ )
{
$tname = $_FILES["f$v"]['tmp_name'];
$name = $_FILES["f$v"]['name'];
$spe = substr($vv['IPath'],strlen($vv['IPath'])-1,1);

if( $spe == '/' )
{
$spe = '';
}
$sname = '..'.$vv['IPath'].$spe.($js+1);
echo '<br/>';
echo "UP_TMP_Name:$tname<br/>UP_NAME:$name<br/>SaveName:$sname<br/>";

if( $name == "" || $tname == "" )
{
continue;
}

if( file_exists($sname) )
{
if( unlink($sname) )
{
echo 'delete exists file '.$name.' ok';
}else{
echo 'cannt delete file.'.$name;
}
}
if( move_uploaded_file($tname,$sname) )
{
echo 'Upload successful!!!(size:'.$_FILES["f$v"]['size'].');<br/>';
$sql = 'update ISource set IContent=IContent+1 where IID='.$vid;
$e = $MySelf->ExeuteSQL($sql);
$js++;
echo 'upload data result:'.$e.'<err>:'.mysql_error().'<br/>';

}else{
$allok = false;
echo $_FILES["f$v"]['error'].'<br/>';
echo 'Upload failed!!!<br/>';
}

}
echo '</div>';
if( $allok )
{
header("Location: MISource.php");
}
}
}
}
mysql_free_result($rel);
}else if($action == 'mod' )
{
$id = $_POST['dd'];
$t = $_POST['t'];
$c = $_POST['c'];
$a = $_POST['a'];
$p = $_POST['p'];
$pp = $_POST['pp'];

$sql = "update ISource set ITitle='$t',IPath='$p',IPassword='$pp',IAuthor='$a',IContent='$c' where IID='$id'";
$rel = $MySelf->ExeuteSQL($sql);
echo '<div class="errinfo">Result:'.$rel.'('.mysql_error().')</div>';
if( "" == mysql_error() )
{
mkdir('..'.$p);
header("Location: MISource.php");
}
}

$gid = $_GET['dd'];
$gid = intval($gid);
$gac = $_GET['a'];
if( $gac == 'dl' && $gid>0 )
{
$MySelf->ExeuteSQL('use '.$SourceDBN.';');
$MySelf->ExeuteSQL("delete from ISource where IID=$gid");
header("Location: MISource.php");
}
?>
<form action="MISource.php" method='post' name='f1'>
<hr width="100%" size="2" color="white" />
SQL:
<input type='text' name='sql' maxlength="500" size='130' /><input value="Execute" type='submit' />
</form>
</div>

<script type="text/javascript">
var attachname = "f";
var i=1;
function addInput(){
if(i>0){
var attach = attachname + i ;
if(createInput(attach))
{ i=i+1;

}

if( navigator.appName.indexOf("Explorer") == -1 ){
document.f3.upcount.setAttribute("value",i);
}else{
document.f3.upcount.value=i;
}
}
document.getElementById('output').innerHTML=document.getElementById('upcount').value;
}
function deleteInput(){
if(i>1){
i=i-1;
if(!removeInput())
{ i=i+1;
}
if( navigator.appName.indexOf("Explorer") == -1 ){
document.f3.upcount.setAttribute("value",i);
}else{
document.f3.upcount.value=i;
}

}
document.getElementById('output').innerHTML=document.getElementById('upcount').value;
}

function createInput(nm){
var aElement=document.createElement("input");
aElement.name=nm;
aElement.id=nm;
aElement.type="file";
aElement.size="50";
aElement.value="30000";
if(document.getElementById("uploadinput").appendChild(aElement) == null)
{ return false;}
var nl = document.createElement("br");
document.getElementById("uploadinput").appendChild(nl);
return true;
}
function removeInput(nm){
var aElement = document.getElementById("uploadinput");
if(aElement.removeChild(aElement.lastChild) == null)
{ return false;}
aElement.removeChild(aElement.lastChild);
return true;
}

</script>

<div align="center">
<table>
<form action="MISource.php" method="post" name="f2">
<tr>
<td colspan="2">Add new</td>
</tr>
<tr><td>Title </td><td><input type="text" value="title" name="title" /></td>
</tr>

<tr>
<td>Author</td>
<td><input type="text" value="Spotatoes" name="author" /></td>
</tr>
<tr>
<td>Path</td>
<td><input type="text" value="/ISource/" name="path" /></td>
</tr>
<tr>
<td>Pwd</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="add" /></td>
<td><input name="reset" type="reset" /></td>
</tr>
<input type="hidden" value="add" name="action" />
</form>
</table>
<hr width="100%" size="2" color="white" />
<table>
<form action="MISource.php" method="post" enctype="multipart/form-data" name="f3">
<tr>
<td><strong><font color="#0000FF">Upload image</font></strong></td>
</tr>
<tr><td><select name="S">
<?
$nnn = $MySelf->GetResult($SourceDBN,'Select * from ISource');
if( $nnn )
{
while( $nny = mysql_fetch_array($nnn,MYSQL_BOTH) )
{
echo "<option value='".$nny[0]."'>".$nny['ITitle']."</option>";
}
}
mysql_free_result($nnn);
?>
</select></td></tr>
<tr>

<td>
<div id="output"></div>
<input type="hidden" value="up" name="action" />
<input type="hidden" value="1" name="upcount" id="upcount"/>
Path:<input type="file" name="f1" value="30000" /><a href="#" onclick="javascript:addInput();">Add More</a>
<a href="javascript:deleteInput();">Delete Last</a>
<br />
<div id="uploadinput">

</div></td>
</tr>
<tr><td><input type="submit" value="Upload" /></td></tr>
</form>
</table>
...全文
762 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wisword 2008-12-08
  • 打赏
  • 举报
回复
var aElement=document.createElement("input");
aElement.name=nm;
aElement.id=nm;
aElement.type="file";
aElement.size="50";
aElement.value="30000";
=====================================
这个要改一下:
try{
var aElement=document.createElement("<input type='file' name='"+nm+"' id='"+nm+"'>");
}catch(e){
var aElement=document.createElement("input");
aElement.name=nm;
aElement.id=nm;
aElement.type="file";
}
aElement.size="50";
aElement.value="30000";
cyxin2121921 2008-12-08
  • 打赏
  • 举报
回复
mark
helloyou0 2008-12-08
  • 打赏
  • 举报
回复
别的没细看,不过不久前才遇过firefox里同样的问题,
你要把<form>和</form>移到<table>外面,不要放在<table>里,
这样firefox里的问题就好了


cuidenghong123 2008-12-07
  • 打赏
  • 举报
回复
把表单定义为数组吧!然后再循环!
mengshan1986 2008-12-07
  • 打赏
  • 举报
回复
给你端js,如果去掉option这个功能就可以兼容火狐,ie,opera,safari,如果不去的只能兼容ie,safari,opera

var i1 = 1;
function addFile(dvID,inputNamePrefix)
{
var dv = document.getElementById(dvID);
var file = document.createElement("label");
file.innerHTML = "第"+i1+"集  ";
var file1 = document.createElement("select");
file1.name = "ftype[]";
file1.id=i1;
file1.onchange = function() {
var as =document.getElementById("file"+file1.id);
var af=document.getElementById("guide"+file1.id);
var a=document.getElementById(file1.id).value;
if(a==1){
as.style.display="none";
af.style.display="inline";
}
else{
as.style.display="inline";
af.style.display="none";
}
}
var file3 = document.createElement("option");
file3.value = "0";
file3.text="远程地址";
var file4 = document.createElement("option");
file4.value = "1";
file4.text="本地上传";
file1.add(file3);
file1.add(file4);
var file2 = document.createElement("input");
file2.type = "text";
file2.id ="file"+i1;
file2.name = "guide1[]";
var file5 = document.createElement("input");
file5.type="file";
file5.name="guide[]";
file5.id="guide"+i1;
file5.style.display="none";
dv.appendChild(file);
dv.appendChild(file1);
dv.appendChild(file2);
dv.appendChild(file5);

var btn = document.createElement("input");
btn.type = "button";
btn.id = btn.name = "btn" + i1;
btn.value = "删除";


btn.onclick = function() {
var b = document.getElementById(btn.id);
dv.removeChild(b.nextSibling); //remove <BR >
dv.removeChild(b.previousSibling); //file
dv.removeChild(b.previousSibling);
dv.removeChild(b.previousSibling);
dv.removeChild(b.previousSibling);
dv.removeChild(b); //btn
}

dv.appendChild(btn);

dv.appendChild(document.createElement("BR"));

i1++;
}

<span id='idfilespan'></span><br />
<a id=attach onclick="addFile('idfilespan','filename')" href="javascript:;">添加内容</a>
mengshan1986 2008-12-07
  • 打赏
  • 举报
回复
给你个现成的,上传的表单name是guide[]是一个数组啊,别写错了

$f_type=strtolower("flv,swf,jpg,png");//设置可上传的文件类型
$file_size_max=500*1024*1024;//限制单个文件上传最大容量500M
$overwrite = 0;//是否允许覆盖相同文件,1:允许,0:不允许
$f_input="guide";//设置上传域名称,数组的名称
$m=0;
foreach($_FILES[$f_input]["error"] as $key => $error){
$up_error="no";
if ($error == UPLOAD_ERR_OK){
if(!$ftype[$m]){
$up_error="yes";

}
$f_name=$_FILES[$f_input]['name'][$key];//获取上传源文件名
$uploadfile=$uploaddir.strtolower(basename($f_name));

$tmp_type=substr(strrchr($f_name,"."),1);//获取文件扩展名
$tmp_type=strtolower($tmp_type);
if(!stristr($f_type,$tmp_type)){
echo "对不起,不能上传".$tmp_type."格式文件, ".$f_name." 文件上传失败!<br

/>";
$up_error="yes";
}

if ($_FILES[$f_input]['size'][$key]>$file_size_max) {
echo "对不起,你上传的文件 ".$f_name." 容量为".round($_FILES[$f_input]

['size'][$key]/1024)."Kb,大于规定的".($file_size_max/1024)."Kb,上传失败!<br />";
$up_error="yes";
}

if (file_exists(".".$uploadfile)&&!$overwrite){
echo "对不起,文件 ".$f_name." 已经存在,上传失败!<br />";
$up_error="yes";
}
$string = 'abcdefghijklmnopgrstuvwxyz0123456789';
$rand = '';
for ($x=0;$x<12;$x++)
$rand .= substr($string,mt_rand(0,strlen($string)-1),1);
$t=date("ymdHis").substr($gettime[0],2,6).$rand;
$attdir=$flash_path.$sort."/";
if(!is_dir(".".$attdir))
{ mkdir(".".$attdir);}
$uploadfile=$attdir.$t.".".$tmp_type;
if(($up_error!="yes") and (move_uploaded_file($_FILES[$f_input]['tmp_name']

[$key],".".$uploadfile))){
$temp[$m]=$uploadfile;//上传文件地址


echo "文件 ".$f_name." 上传成功!<br />";
}
}

$m++;


}
starrychen 2008-12-06
  • 打赏
  • 举报
回复
你是想用php同时上传多个文件吗,写个循环就行了,我写过的,要是用到js那我就不怎么会了。你先把意图说清楚!!!!
jim8590251 2008-12-06
  • 打赏
  • 举报
回复

//上传附件
if($fjor == "yes") //选择了上传附件
{
for($i=0;$i<$fjnum;$i++)
{
$fj_name ="list_fj".$i;

$fj_relname = "fj_name".$i;
$myfjname = $_POST[$fj_relname];
if($$fj_name!="")
{
include_once("../inc/class/upload.php");
$url="../UserFiles/".date('Ym')."/";
mkdir($url,0777);
$f=new upfile($url,"doc,rar,zip,ppt,xls");
if(isset($_FILES[$fj_name]))
{

if($f->upload($fj_name))
{

$myfj="UserFiles/".date('Ym')."/".$f->getname();
}

}
//插入附件表
$sqlf="insert into list_fj(userid,listid,fj_name,fj_url,begindate,modifydate) values(".$_SESSION['adminchecked'].",".$rsz[0]["id"].",'".$myfjname."','".$myfj."','".date('Y-m-d H:i:s')."','".date('Y-m-d H:i:s')."')";
//echo $sqlf;die();
$DB->Query($sqlf);
}

}
}

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧