PHP脚本将图像文件插入到mysql表中

时间:2015-08-26 09:54:12

标签: php mysql

在PHP上仍然很弱,我需要一些帮助,请在我的脚本上,我已经尝试在表格中插入图像和文本,文本很好地张贴在表格中并正确显示但图像显示" C: \ fakepath \ Logo Micro Connection Finale.jpg "在浏览器上。 我将逐步向您展示我使用的脚本文件。

1:这是" manageales.php"文件保存HTML代码

Machine info (lscpu):

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    2
Core(s) per socket:    4
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 58
Model name:            Intel(R) Core(TM) i7-3612QM CPU @ 2.10GHz
Stepping:              9
CPU MHz:               2889.878
CPU max MHz:           3100.0000
CPU min MHz:           1200.0000
BogoMIPS:              4192.97
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              6144K
NUMA node0 CPU(s):     0-7

2:这是" footer.php"文件保存javascript代码

 <div id="tab5s">
  <div class="units-row" style="font-size: 12px">
   <table class="table table-hovered table-bordered table-striped unit-100">
    <tr class="unit-100 message message-info">
   <th colspan="2"> <font size="3px">Company/Office Creation</font></th>
   <tr class="unit-100" style="">
    <td> Name.</td>
      <td><input type="text" id="name" name="name"</td>*</td>
    </tr>
    <tr class="unity-100" style="">
<td>Tax identification number</td>
 <td><input type="text" id="tin" name="tin"></td>
</tr>
 <tr class="unity-100" style="">
  <td> Address. </td>
  <td><input type="text" id="adress" name="adress"/>&nbsp;&nbsp;</td>
     </tr>
   <tr class="unity-100" style="">
 <td> Photo </td>
  <td><input name="FILE" value="102400" type="hidden">
 <input id="image" name="image" accept="image/jpeg" type="file">
  </td></tr><td><button class="btn btn-round " id="tupia">Save</button>
    </td>
         </table></div>

3:这是&#34; company_junction.php&#34;文件

 $('#tupia').click(function(){
         var name   = $('#name').val();
         var tin   = $('#tin').val();
         var adress  = $('#adress').val();
         var sta  = $('#sta').val();
         var tel  = $('#tel').val();
         var mob  = $('#mob').val();
         var email  = $('#email').val();
         var curence  = $('#curence').val();
         var image  = $('#image').val();

         $('#name').removeClass('input-error');
         $('#adress').removeClass('input-error');
         $('#mob').removeClass('input-error');

         if(name=="" && adress=="" && mob==""){
             $('#name').addClass('input-error');
             $('#adress').addClass('input-error');
             $('#mob').addClass('input-error');
         }else if(name==""){
             $('#name').addClass('input-error');
         }else if(adress == ""){
             $('#adress').addClass('input-error');
         }else if(mob == ""){
             $('#mob').addClass('input-error');
         }else{
             $('<span />',{ text: 'Please waiting .' , id:'fd'}).css({'color':'red', 'padding-left':'12px', 'font-size': '12px'}).insertAfter(this);
             $('#fdpr').fadeIn();
             $.post('company_junction', {name:name, tin:tin, adress:adress, state:sta, tel:tel, mob:mob, email:email, curence:curence, image:image}, function(data){
                 var fd = $('#fd');
                 $(fd).hide();

                 var res = data;
                 if(res == 'good'){
                     $('#name').val('');
                     $('#tin').val('');
                     $('#adress').val('');
                     $('#sta').val('');
                     $('#tel').val('');
                     $('#mob').val('');
                     $('#email').val('');
                     $('#curence').val('');
                     $('#image').val('');
                     $('#fdpr').removeClass('message').removeClass('message-error');
                     $('#fdpr').addClass('message').addClass('message-success').html("Information saved successfully").fadeOut(4000);

                 }else if(res == "stop"){
                     $('#fdpr').removeClass('message').removeClass('message-success');
                     $('#fdpr').addClass('message').addClass('message-error').html("This Office name Or adress already exist").fadeOut(3000);
                 }else{
                     $('#fdpr').removeClass('message').removeClass('message-success');
                     $('#fdpr').addClass('message').addClass('message-error').html('Information not saved').fadeOut(4000);
                 }
             });


         }

     });

4:最后一个是&#34; save_company.php&#34;文件,用于将信息发布到mysql中。

 <?php
 require_once 'incs/functions.php';
 require_once 'classes/Database.php';
 echo save_company::savecompany_info();
 ?>

这是mysql表字段

 <?php
    class save_company{
      public static function savecompany_info(){

    global $database;

    $item = $database->escape_value($_POST['name']);
    $tn = $database->escape_value($_POST['tin']);
    $price = $database->escape_value($_POST['adress']);
    $sta = $database->escape_value($_POST['state']);
    $tel = $database->escape_value($_POST['tel']);
    $mob = $database->escape_value($_POST['mob']);
    $email = $database->escape_value($_POST['email']);
    $cure = $database->escape_value($_POST['curence']);
    $image = $database->escape_value($_POST['image']);
    $date = date('Y-m-d');

    //just check if name exist in the company_info table
    $se = "SELECT * FROM company_info";
    $QR =$database->query($se);
    $ft =$database->fetch_array($QR);
    $ans =$ft['name'];
    $ans2 =$ft['adress'];
    if($ans == $item || $ans2 == $price){
        echo "stop";
    }else{
        $sql2 = "INSERT INTO company_info VALUES('','$item','$tn','$price','$sta','$tel','$mob','$email','$cure','$image')";
        $res3 = $database->query($sql2);
        if ($res3) {
            echo "good";
        } else {
            echo "stop";
        }
    }
  }
}

存储图像的图像字段的数据类型是&#34; MEDIULBLOB&#34;。

2 个答案:

答案 0 :(得分:0)

图像未保存在数据库中。仅图像路径保存在数据库中。通过它你可以显示该图像。一般来说你做什么。因此,请在表格中保存此“文件路径”。在下面的示例中,$ RTargetPath就在那里,它有Filepath。因此,'UploadImage'是保存文件的文件夹。所以,使用此代码。这将有所帮助。

function GetPropertyImageExtension($imagetype)
{
   if(empty($imagetype)) return false;
   switch($imagetype)
   {
      case 'image/bmp': return '.bmp';

      case 'image/gif': return '.gif';

      case 'image/jpeg': return '.jpg';

      case 'image/png': return '.png';

      default: return false;

   }
}
if (!empty($_FILES['CustomImage']["name"]))
{   
   $file_name=$_FILES['CustomImage']["name"];
   $temp_name=$_FILES['CustomImage']["tmp_name"];
   $imgtype=$_FILES['CustomImage']["type"];
   $ext= GetPropertyImageExtension($imgtype);
   $imagename=date("d-m-Y")."-".time().$ext;

   $Rtarget_path = "UploadImage/".$imagename;
   if(move_uploaded_file($_FILES['CustomImage']['tmp_name'], $Rtarget_path ))
   {
      [..SQL Query..]
   }
}

答案 1 :(得分:0)

不建议在MySQL系统中将图像存储在数据库中。 ,因为它会在您的系统中产生不必要的开销。如果您确实要将图像存储在mysql数据库中,请参阅此Tutorial