PHP多图上传取值有关问题

WBOY
풀어 주다: 2016-06-13 12:11:59
원래의
881명이 탐색했습니다.

PHP多图上传取值问题
搞了一个多图上传,INPUT里指定了3个上传图片功能,自已搞了一下,发现上传成功了,图1能自动改名,图2和3不能自动改名,请问要怎样改?

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

------解决思路----------------------
這裡有問題, 不應該是$filename[0],$filename[1],$filename[2];而應該是$filename[0],$filename2[0],$filename3[0]
<br />do <br />{ <br />$filename[0]=random(10); //设置随机数长度 <br />$name=implode(".",$filename); <br />//$name1=$name.".Mcncc"; <br />$uploadfile=$uploaddir.$name; <br />$filename[1]=random(10); //设置随机数长度 <br />$name2=implode(".",$filename2); <br />//$name1=$name.".Mcncc"; <br />$uploadfile2=$uploaddir.$name2; <br />$filename[2]=random(10); //设置随机数长度 <br />$name3=implode(".",$filename3); <br />//$name1=$name.".Mcncc"; <br />$uploadfile3=$uploaddir.$name3; <br />}<br />
로그인 후 복사


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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿