从数据库显示的图像损坏

时间:2017-03-28 07:43:16

标签: php mysql html5

大家好,非常感谢你们给予的任何帮助。这里的代码是使用表单将img上传到mysql数据库,这是将照片上传到数据库的代码。

数据库

CREATE TABLE `user_pic` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `img` LONGBLOB NOT NULL,
    `img_name` VARCHAR(200) NOT NULL,
    `upload_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `user_id` INT(11) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB

upload_img.php

  require ('functions.php');
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $conexion = db_cnt('user_login', 'root', '');
    if (!$conexion) {
      die('Could not connect: ' . mysql_error());
    }


    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];

      // Read the file
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);

      // Create the query and insert
      // into our database.
      $statement = $conexion->prepare("INSERT INTO user_pic (img) VALUES ('$data')");
      $statement->execute(array(
            ':img' => $data
        ));
    }
    else {
      print "No image selected/uploaded";
    }
  }

在这里我循环所有图像并在html上显示图像

gallery.php

<?php
  require ('../include/upload_img.php');

  $conexion = db_cnt('user_login', 'root', '');
  $statement = $conexion->prepare("SELECT * FROM user_pic");
  $statement->execute();
  $fotos = $statement->fetchAll();

?>
            <?php foreach($fotos as $foto):?>
            <div class="col-xs-12 col-sm-4 col-md-3 item-photo">
              <div class="photo">
                <img class="img-responsive" src="<?php echo $foto['img'] ?>" alt="">
                <a class="search zoom fancybox" href="../img/992.jpg"><span class="icon-search"></span></a>
                <a class="star" href="#"><span class="icon-star"></span></a>
                <a class="download" href="#"><span class="icon-download"></span></a>
              </div>
            </div>
            <?php endforeach;?>

但问题是只能向我展示破碎的图像,我不知道会出现什么问题,如果你能提供一些很好的帮助,那么感谢你们这么多人。

0 个答案:

没有答案