>  기사  >  백엔드 개발  >  一段下传图片,并在MYSQL中记录路径的代码求指正

一段下传图片,并在MYSQL中记录路径的代码求指正

WBOY
WBOY원래의
2016-06-13 10:36:01688검색

一段上传图片,并在MYSQL中记录路径的代码求指正
只上传一张图片的功能已经实现,上传两张图片就实现不了,代码段比较简单,麻烦大家指导一下:

上传图片的表单:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?phpif ( isset($_GET['action']) && $_GET['action'] == "save" ){include_once('conn.php');  //连接数据库include_once('uploadclass.php');//这个文件在下文会给出$pic=$uploadfile;   //第一张图片,已经可以正常上传$pic2=$uploadfile2; //第二张,上传不了$sql="insert into product (pic,pic2) values ('$pic','$pic2')";$result=mysql_query($sql,$conn);if   (!$result){      echo mysql_error();      echo $sql;      exit;}else{      echo"<Script>window.alert('congratulations,the product data inserted successful');location.href='index.php'";} mysql_close();}?><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Administrator for upload the product resource</title>
Images_1:
Images_2:



上传程序:uploadclass.php(此程序只能上传图片1,如果要能上传图片2,需要怎么加代码呢?)
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $uploaddir = "../images/upfiles/";$type=array("jpg","gif","bmp","jpeg","png");$patch="aloesky/";function fileext($filename){return substr(strrchr($filename, '.'), 1);}function random($length){$hash = 'CR-';$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['file']['name']));if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type)){$text=implode(",",$type);echo "You can only upload the following types of files:",$text,"<br /> <a href="upload_pro.php">return and try again?click me</a>";exit;}else{$filename=explode(".",$_FILES['file']['name']);do{$filename[0]=random(10); $name=implode(".",$filename);//$name1=$name.".Mcncc";$uploadfile=$uploaddir.$name;}while(file_exists($uploadfile));if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){if(is_uploaded_file($_FILES['file']['tmp_name'])){echo "the Images Upload failed!";}else{echo "<center>Your file has been uploaded: </center><br><center><img  src="%24uploadfile" alt="一段下传图片,并在MYSQL中记录路径的代码求指正" ></center>";echo "<br><center><a href="upload_pro.php">Continue to upload</a></center>";}}}?>

 

------解决方案--------------------
探讨

引用:


这样当成数组就好处理了。
php端: print_r($_FILES['file']); //得到一个二维数组,只需处理数组即可.
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.