一个图片上传得例子php+mysql+apache

    技术2022-05-11  90

    main.php

    --------------------------------------------

    <?php header("content-type:text/html; charset=utf-8"); require ("config.php"); require ("class.php"); $db=new db; $db -> connect_db($dbhost,$dbuser,$dbpassword,$dbname); //$sql="select * from zhuce"; //$row=$db -> query($sql); //while ($result=$db -> mysql_row($row)){  //print_r ($result) ; //} $textname = $_POST['textname']; $picname=$_FILES['filename']['name']; //$db=new db; $name=$db->picup($picname); $end=substr($name , 1); if(strpos($type , $end)){ //echo $textname ; //echo $name; //echo "<br>"; $uppicname=(string)date("Y").(string)date("m").(string)date("d").(string)date("h").(string)date("i").(string)date("s").$name; //echo $uppicname ; $picname=$uppicname; $uploadfile=$filename.$picname ; if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)){  $sql_up="INSERT INTO `photo` SET `filename`='$uppicname', `textname`='$textname'";  if($db->query($sql_up)){   echo "上传成功";   header("location:pic.php");

       }else{    "上传失败";   }

     } }else{  echo "上传得格式不正确";

     }?> ----------------------------------------------

    config.php

    <?php $dbhost="localhost"; $dbuser="root"; $dbpassword="1981"; $dbname="test"; $filename="./photo/"; $type="gif,bmp,jpeg,swf,png,jpg,htm"; //设置允许上传文件的类型?>

    ------------------------------------------------

    upload.php

    <html><head><title>带提取码文件下载工具</title><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link href="du.css" type=text/css rel=stylesheet></head><body leftmargin="60"><?php require 'global.php'; require 'config.php'; require 'db.php'; $photoname=$_POST['photoname']; /*判断文件类型*/ if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type)){  $text=implode(",",$type);  echo "您只能上传以下类型文件: ",$text,"<br>"; }else{ /*生成目标文件的文件名*/ $filename=explode(".",$_FILES['filename']['name']);  do{   //给文取名 $photo_name   $photo_name=(string)date("y") .(string)time("D");   $filename[0]=$photo_name; //设置随机数长度   $name=implode(".",$filename);   $uploadfile=$uploaddir.$name;  }while(file_exists($uploadfile));  if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)){      $db=new db;   $db->connect($dbhost, $dbuser, $dbpwd, $dbname);   $sql="insert into list (photoname,file) values ('$photoname','$name')";    if($db->query($sql)){     echo "上传完成";    }else{     echo "upload failed";    }  } }?><p><< <a href="./main.htm"><img src="back.gif" width=37 height=12 border="0"></a></p></body></html>

    -------------------------------------------------

    前台页面

    pic.php

    <?php header("content-type:text/html; charset=utf-8");

    ?><body ><form method="post" action="mian.php" enctype="multipart/form-data" name="leave_word">

    <table align="center" style="margin-top:50px;">

      <tr>  <td style="font-size:12px;">上传文件:</td>  <td height="23"><input type="file" name="filename" style="height:20px; line-height:16px; width:300px;"> </td>  </tr>  <tr>  <td style="font-size:12px;">描述:</td>  <td height="23"><textarea name="textname" style="height:100px; width:300px;"></textarea></td>  </tr>  <tr>  <td></td><td height="23" align="center">  <input type="image" src="images/input01.gif" />  <img src="images/input02.gif" style="cursor:hand" onClick="document.all.leave_word.reset()" /></td> </tr></table>

    </form></body>------------------------------------------------

    photo.sql

    CREATE TABLE `photo` (  `id` int(11) NOT NULL auto_increment,  `filename` varchar(20) default NULL,  `textname` text,  PRIMARY KEY  (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    -----------------------end--------------------------------

     


    最新回复(0)