<?php
if ($submit) {
    MYSQL_CONNECT( "localhost", "sa", "");
    mysql_select_db( "binary_data");    $data = addslashes(fread(fopen($form_data,  "r"), filesize($form_data)));    $result=MYSQL_QUERY( "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
         "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
echo "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')";
    $id= mysql_insert_id();
    print  "<p>This file has the following Database ID: <b>$id</b>";    MYSQL_CLOSE();} else {     ?>
<META content="text/html; charset=big5" http-equiv=Content-Type>    <form method="post" action=" <?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="form_description"  size="40">
    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form><?php}?></BODY>
</HTML> <?//  getdata.php3?id= 
if($id) {  @MYSQL_CONNECT( "localhost", "sa", ""); 
 @mysql_select_db( "binary_data"); 
 $query =  "select bin_data,filetype from binary_data where id=$id"; 
 $result = @MYSQL_QUERY($query); 
 $data = @MYSQL_RESULT($result,0, "bin_data"); 
 $type = @MYSQL_RESULT($result,0, "filetype"); 
Header(  "Content-type: $type"); 
echo $data; 
echo $type;
}; 
?> 这个程序在用php4+apache+window2000+mysql通过

解决方案 »

  1.   

    试试看,下面的代码,我自己写的,在PHP4+APACHE+MYSQL上通过
    图片和图片文字说明是从客户端通过表单(form)的文件框上传的.<?
        $link=@mysql_connect("localhost","yrname","yrpass");
        //连接,用你的具体连接名替换yrname,具体连接密码替换yrpass
            if($link==false) 
               {
               print "<br><br><br>";
               print "<p><b>连接数据库时发生错误,请稍后再试!</b></p>";
               exit();
               }
        
        $qu="use yrbase";  
        // 用你的具体数据库代替yrbase
        $res=@mysql_query($qu,$link);  
            if($res==false) 
               {
               print "<br><br><br>";
               print "<p><b>打开数据库时发生错误,请稍后再试!</b></p>";
               exit();
               }          $tmpset=0;  
      // 存放成功加入的图片数目  for($i=1;$i<=4;$i++)
          { 
          // picfile[i]存放所提交的图片信息(文件路径)
          // none表示用户没有在这个文件上传框中选择文件
          if(trim($picfile[$i])!="none")
             {
             //读取数据
             $fp=fopen($picfile[$i],"r");
             $picdata=fread($fp,filesize($picfile[$i]));
             fclose($fp);
            
             //加上必要的标志符号
             $picdata=addslashes($picdata);
             
             //用具体的数据表名代替yrpict
             //pictext[i]存放所提交的图片的文字说明
             $qu="insert into yrpict(picid,picdata,pictext) values(null,'$picdata','$pictext[$i]')";
             $res=@mysql_query($qu,$link);  
                   if($res==false) 
                       {
                       print "<br><br>";
                       print "<p><b>图片&nbsp;";
                       echo $i;
                       print "&nbsp;提交失败!</b></P>";
                       continue;
                       }           $tmpset=$tmpset+1;                                 
             }       }  print "<br><br>"; 
      print "<p><b>操作成功!</b></p>";  
      print "<p><b>实际入库图片数&nbsp;";
      echo $tmpset;
      print "&nbsp;</b></P>";
      exit();  }?>
      

  2.   

    (1)表单页面加入以下enctype关键字:
        <form name="formname" method="post" action="pictintodb.php"
         enctype="multipart/form-data">
       用实际的表单名代替formname,用实际的图片入库程序名代替pictintidb.php
    (2)图片数据类型是LONGBLOB(3)以下是小七小妹写的图片显示的相关主要代码:前台的图片放置页面的关键语句:
    <?
     print"<img src=\"showpict.php?picid=$pictid\" width=\"236\" height=\"154\">";
    ?>后台的showpict.php大致如下:
    <? 
    $link=@mysql_connect("localhost","XXX","XXX"); $qu="use YYY"; 
    $res=@mysql_query($qu,$link);
      
    $qu="select picid,picdata from npict where picid=$picid"; 
    $res=@mysql_query($qu,$link); $num=mysql_num_rows($res);
       if($num==0) 
          { 
          print "<br><br><br>";
          print "<p><b>没有这张图片!</b></p>";
          exit();  
          }$row=@mysql_fetch_row($res);  
     
    header("Content-type:image/");echo $row[1];exit();
    ?>
      

  3.   

    请问如果不是模块化安装的PHP。
    即“Server API”是CGI的那么header()函数是不是没有用。