把图片的路径存到 mysql中,把图片传到相应的路径下面!

解决方案 »

  1.   

    ccterran(iwind) 的代码简洁明了。推荐。
    http://expert.csdn.net/Expert/topic/2977/2977700.xml?temp=.0338251
    存到目录的看看$_FILES[]和COPY用法就行了。
      

  2.   

    上传后将文件名写入数据库的例子,假设表名pic,字段id,picname,pic
    1.php
    <form action="2.php" method="post" enctype="multipart/form-data">
    <table width="98%"  border="0" cellspacing="1" cellpadding="0">
      <tr align="left">
        <td colspan="2"><? echo $_GET['message'];?></td>
        </tr>
      <tr>
        <td width="25%" align="right">名称</td>
        <td width="75%" align="left"><input name="name" type="text" id="name"></td>
      </tr>
      <tr>
        <td align="right">图片</td>
        <td align="left"><input type="file" name="image"></td>
      </tr>
      <tr>
        <td align="right">&nbsp;</td>
        <td align="left"><input type="submit" name="Submit" value="提交"></td>
      </tr>
      <tr>
        <td align="right">&nbsp;</td>
        <td align="left">&nbsp;</td>
      </tr>
    </table>
    </form>2.php
    <?
    //函数,用于产生id;
    function makekey()
    {
           $a=array(" ",".");
    $tf=str_replace($a,"",microtime());
    return $tf;
    }
    //连数据库。
    $hostname_myconn = "localhost";
    $database_myconn = "pic";
    $username_myconn = "root";
    $password_myconn = "";
    $myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn);
    mysql_select_db($database_myconn);
    //开始处理
    $name=$_POST['name'];
    if (empty($name))
    {
    header("location:1.php?message=请输入名称");
    exit;
    }
    $image=$_FILES['image']['name'];
    $size=$_FILES['image']['size'];
    $type=$_FILES['image']['type'];
    //检查文件类型及大小。
    if( ($type == "image/gif" || $type == "image/png"  || $type == "image/pjpeg")&& $size<200000)
      {   
           $a=array(" ",".");
    //产生文件名
    $tf=str_replace($a,"",microtime()).".".substr($image,-3);
    //文件保存在pic子目录下
    $dest_image = "pic/".$tf; 
    if (!move_uploaded_file($_FILES['image']['tmp_name'], $dest_image))
         {
    $tf = "no";  
    }
       }
       else
       {
    header("location:1.php?message=请选择文件上传,或者你选择的文件不符合要求");
    exit;
       }
       $id=makekey();
       //存入数据库
    $str="insert into pic (id,picname,pic) values ('$id','$name','$tf')";
    mysql_query($str);
    //显示信息或重定向页面
    echo "保存完成";
    ?>