Home > Article > Backend Development > PHP多图上传取值问题
搞了一个多图上传,INPUT里指定了3个上传图片功能,自已搞了一下,发现上传成功了,图1能自动改名,图2和3不能自动改名,请问要怎样改?
$uploaddir = "uploadfiles/";//设置文件保存目录 注意包含/ $type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型 $patch="uploadfiles/";//程序所在路径 //获取文件后缀名函数 function fileext($filename) { return substr(strrchr($filename, '.'), 1); } //获取文件后缀名函数 function fileext2($filename2) { return substr(strrchr($filename2, '.'), 1); } //获取文件后缀名函数 function fileext3($filename3) { return substr(strrchr($filename3, '.'), 1); } //生成随机文件名函数 function random($length) { $hash = 'java-'; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen($chars) - 1; mt_srand((double)microtime() * 1000000); for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } $a=strtolower(fileext($_FILES['img1']['name'])); $b=strtolower(fileext2($_FILES['img2']['name'])); $c=strtolower(fileext3($_FILES['img3']['name'])); //判断文件类型 if(!in_array(strtolower(fileext($_FILES['img1']['name'])),$type)) { $text=implode(",",$type); echo "您只能上传以下类型文件: ",$text,"<br>"; } //生成目标文件的文件名 else{ $filename=explode(".",$_FILES['img1']['name']); $filename2=explode(".",$_FILES['img2']['name']);$filename3=explode(".",$_FILES['img3']['name']);do { $filename[0]=random(10); //设置随机数长度 $name=implode(".",$filename); //$name1=$name.".Mcncc"; $uploadfile=$uploaddir.$name; $filename[1]=random(10); //设置随机数长度 $name2=implode(".",$filename2); //$name1=$name.".Mcncc"; $uploadfile2=$uploaddir.$name2; $filename[2]=random(10); //设置随机数长度 $name3=implode(".",$filename3); //$name1=$name.".Mcncc"; $uploadfile3=$uploaddir.$name3; } while(file_exists($uploadfile)); if (move_uploaded_file($_FILES['img1']['tmp_name'],$uploadfile)){ //if(is_uploaded_file($_FILES['file']['tmp_name'])){ //输出图片预览 //echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile' alt="PHP多图上传取值问题" ></center>"; //echo"<br><center><a href='javascript:history.go(-1)'>继续上传</a></center>"; //} //else{ //echo "上传失败!"; } if (move_uploaded_file($_FILES['img2']['tmp_name'],$uploadfile2)){ //if(is_uploaded_file($_FILES['file']['tmp_name'])){ //输出图片预览 //echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile' alt="PHP多图上传取值问题" ></center>"; //echo"<br><center><a href='javascript:history.go(-1)'>继续上传</a></center>"; //} //else{ //echo "上传失败!"; }if (move_uploaded_file($_FILES['img3']['tmp_name'],$uploadfile3)){ //if(is_uploaded_file($_FILES['file']['tmp_name'])){ //输出图片预览 //echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile' alt="PHP多图上传取值问题" ></center>"; //echo"<br><center><a href='javascript:history.go(-1)'>继续上传</a></center>"; //} //else{ //echo "上传失败!"; }} $SourceFileName=$uploadfile;$SourceFileName2=$uploadfile2;$SourceFileName3=$uploadfile3;
??有??, 不??是$filename[0],$filename[1],$filename[2];而??是$filename[0],$filename2[0],$filename3[0]
do { $filename[0]=random(10); //设置随机数长度 $name=implode(".",$filename); //$name1=$name.".Mcncc"; $uploadfile=$uploaddir.$name; $filename[1]=random(10); //设置随机数长度 $name2=implode(".",$filename2); //$name1=$name.".Mcncc"; $uploadfile2=$uploaddir.$name2; $filename[2]=random(10); //设置随机数长度 $name3=implode(".",$filename3); //$name1=$name.".Mcncc"; $uploadfile3=$uploaddir.$name3; }
<?php$uploaddir = "uploadfiles/"; //设置文件保存目录 注意包含/$type = array( "jpg", "gif", "bmp", "jpeg", "png"); //设置允许上传文件的类型$patch = "uploadfiles/"; //程序所在路径print_r($_FILES);//获取文件后缀名函数function fileext($filename) { return substr(strrchr($filename, '.') , 1);}//获取文件后缀名函数function fileext2($filename2) { return substr(strrchr($filename2, '.') , 1);}//获取文件后缀名函数function fileext3($filename3) { return substr(strrchr($filename3, '.') , 1);}//生成随机文件名函数function random($length) { $hash = 'java-'; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen($chars) - 1; mt_srand((double)microtime() * 1000000); for ($i = 0; $i < $length; $i++) { $hash.= $chars[mt_rand(0, $max) ]; } return $hash;}$a = strtolower(fileext($_FILES['img1']['name']));$b = strtolower(fileext2($_FILES['img2']['name']));$c = strtolower(fileext3($_FILES['img3']['name']));//判断文件类型if (!in_array(strtolower(fileext($_FILES['img1']['name'])) , $type)) { $text = implode(",", $type); echo "您只能上传以下类型文件: ", $text, "<br>";}//生成目标文件的文件名else { $filename = explode(".", $_FILES['img1']['name']); $filename2 = explode(".", $_FILES['img2']['name']); $filename3 = explode(".", $_FILES['img3']['name']); do { $filename[0] = random(10); //设置随机数长度 $name = implode(".", $filename); //$name1=$name.".Mcncc"; $uploadfile = $uploaddir . $name; $filename2[0] = random(10); //设置随机数长度 $name2 = implode(".", $filename2); //$name1=$name.".Mcncc"; $uploadfile2 = $uploaddir . $name2; $filename3[0] = random(10); //设置随机数长度 $name3 = implode(".", $filename3); //$name1=$name.".Mcncc"; $uploadfile3 = $uploaddir . $name3; } while (file_exists($uploadfile)); if (move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile)) { //if(is_uploaded_file($_FILES['file']['tmp_name'])){ //输出图片预览 //echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile' alt="PHP多图上传取值问题" ></center>"; //echo"<br><center><a href='javascript:history.go(-1)'>继续上传</a></center>"; //} //else{ //echo "上传失败!"; } if (move_uploaded_file($_FILES['img2']['tmp_name'], $uploadfile2)) { //if(is_uploaded_file($_FILES['file']['tmp_name'])){ //输出图片预览 //echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile' alt="PHP多图上传取值问题" ></center>"; //echo"<br><center><a href='javascript:history.go(-1)'>继续上传</a></center>"; //} //else{ //echo "上传失败!"; } if (move_uploaded_file($_FILES['img3']['tmp_name'], $uploadfile3)) { //if(is_uploaded_file($_FILES['file']['tmp_name'])){ //输出图片预览 //echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile' alt="PHP多图上传取值问题" ></center>"; //echo"<br><center><a href='javascript:history.go(-1)'>继续上传</a></center>"; //} //else{ //echo "上传失败!"; }}$SourceFileName = $uploadfile;$SourceFileName2 = $uploadfile2;$SourceFileName3 = $uploadfile3;?>
原来那里设错了,谢谢了!