在数据库中上传图片

时间:2017-11-19 14:46:44

标签: php mysql

我在撰写简短的PHP代码时遇到了问题: 我有这个代码,我发送到数据库任何字符串,并在同一页面上它回显任何字符串。 我试图插入一个图像,但我没有上诉,你可以解决这个问题吗? 这是代码:

<form action="" method="POST">
<input type="text" name="namef" class="namef" placeholder="scrivi il tuo      nome"><BR></BR>
<textarea name="data" class="rounded" placeholder="Condividi il tuo     pensiero con il mondo."></textarea><BR></BR>
<input type="submit" value="Pubblica!" name="invia">
<input type="file" name="uph">
</form>

<?php
if($_POST['data']){
$nome=$_POST['namef'];
$data=$_POST['data'];
$ph=$_FILES['uph'];
$conn=mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('post', $conn);
mysql_query("INSERT INTO post (name, post, photo) VALUES ('$nome',   '$data', '$ph') ") or die(mysql_error());
}
?>

<div class="mann"><?php
$conn=mysql_connect('localhost', 'root', '') or die();
mysql_select_db("post", $conn);
$result= mysql_query("SELECT * FROM post") or die();
while($row= mysql_fetch_array($result)){
echo $row["name"]. " ha pubblicato un nuovo commento:". "</br>".       $row["post"]. "</br". $row["photo"]. "</br>". "</br>". "</br>";
}
?></div>

1 个答案:

答案 0 :(得分:0)

首先我通过'type'找到图像的类型 如果这不起作用那么'mime' 如果这不起作用,我创建一个新图像,将上传的文件视为gif,png和jpg,并检查哪个转换有效。

在保存到数据库之前,我将图像从二进制转换为base64。为此,我需要图像类型。

$thumbWidth = 60;
$thumbHeight = 60;
$previewWidth = 80;
$previewHeight = 60;
$bigWidth = 320;
$bigHeight = 240;
$save = false;
$num = 1;
if( is_uploaded_file($_FILES['image1']['tmp_name']) || !($_FILES['image1']['error'] !== UPLOAD_ERR_OK)){
  $save = false;
  $jspan = '';
  $gspan = '';
  $pspan = '';
  switch(strtolower($_FILES['image1']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = @getimagesize($_FILES['image1']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['image1']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
}

然后我缩放图像

  $originalWidth  = imagesx($image);
  $originalHeight = imagesy($image);
  $scale      = min($previewWidth/$originalWidth,$previewHeight/$originalHeight);
  $newWidth  = ceil($scale*$originalWidth);
  $newHeight = ceil($scale*$originalHeight);
  $newPic = imagecreatetruecolor($newWidth, $newHeight);
  imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);

然后我转换为gif,png和jpg,找到哪个是最紧凑的。

  ob_start();
  imagejpeg($newPic, NULL, 70);
  $jpg = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $jLen = strlen($jpg);
  ob_start();
  imagepng($newPic, NULL, 9);
  $png = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $pLen = strlen($png);
  ob_start();
  imagegif($newPic);
  $gif = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $gLen = strlen($gif);
  if ($gLen < $jLen){
    if ($pLen < $gLen){
      $raw = $png;
      $png = null;
      $type = 'png';
      $pspan = '<span class="size">';
    }
    else{
      $raw = $gif;
      $gif = null;
      $type = 'gif';
      $gspan = '<span class="size">';
    }
  }
  elseif($pLen < $jLen){
    $raw = $png;
    $png = null;
    $type = 'png';
    $pspan = '<span class="size">';
  }
  else{
    $raw = $jpg;
    $jpg = null;
    $type = 'jpg';
    $jspan = '<span class="size">';
  }

然后我使用$typedata:image/$type

转换为base64
  $base64 = base64_encode($raw);
  $thumb1 = "#thumb1{background-image:url('data:image/$type;base64,$base64')}";

在将base64图像保存到数据库之前,我将其转义。

$thumb1 = mysqli_real_escape_string($con,$thumb1);