PHP多图上传取值有关问题

WBOY
Freigeben: 2016-06-13 12:11:59
Original
881 Leute haben es durchsucht

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;
Nach dem Login kopieren

------解决思路----------------------
這裡有問題, 不應該是$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 />
Nach dem Login kopieren


改好后的程序
<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 />
Nach dem Login kopieren

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage