Php 当我从数据库下载Blob时,它的大小是1kb

Php 当我从数据库下载Blob时,它的大小是1kb,php,image,download,Php,Image,Download,当我从数据库下载一个blob图像时,它的大小是1kb。当我打开.png文件时,图像没有出现。图像大小显示在my MYSQL数据库表中。我不明白为什么它会在名称和文件类型正确显示的情况下从我的数据库下载一个空文件 我的表列 名称/文件类型/大小似乎都是正确的 Upload.php <?php require('config.php'); session_start(); if(isset($_POST['save'])) { $target_dir = "upload/img/";

当我从数据库下载一个blob图像时,它的大小是1kb。当我打开.png文件时,图像没有出现。图像大小显示在my MYSQL数据库表中。我不明白为什么它会在名称和文件类型正确显示的情况下从我的数据库下载一个空文件

我的表列

名称/文件类型/大小似乎都是正确的

Upload.php

<?php

require('config.php');
session_start();
if(isset($_POST['save']))
{
  $target_dir = "upload/img/";

  $filename = explode('.',$_FILES['image']['name']);
  $ext = $filename[1];
  $imgname = time().'.'.$ext;
  $target_file = $target_dir . $imgname ;

  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  // Check if image file is a actual image or fake image
  $check = getimagesize($_FILES["image"]["tmp_name"]);
  if($check !== false) {
    $text="File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    $text="File is not an image.";
    $uploadOk = 0;
  }
  // Check if file already exists
  if(file_exists($target_file)) {
    $text="Sorry, file already exists.";
    $uploadOk = 0;
  }
  // Check file size
  if($_FILES["image"]["size"] > 2000000) {
    $text="Sorry, your file is too large.";
    $uploadOk = 0;
  }
  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  && $imageFileType != "gif" && $imageFileType != "bmp" ) {
    echo "Sorry, only JPG, JPEG, PNG, GIF & BMP files are allowed.";
    $uploadOk = 0;
  }
  // Check if $uploadOk is set to 0 by an error
  if($uploadOk == 0) {
    $_SESSION["error"]=$text;
    header("Location:index.php?id=$id"); /* Redirect browser */
    exit();

  // if everything is ok, try to upload file
  }else{
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
      $path=$imgname;
      $finfo = new finfo();
        $array = explode('.', $_FILES['image']['name']);
        $extension = end($array);
        $filesize=$_FILES["image"]["size"];

      $conn->query("INSERT INTO images (image, name,filetype,size) VALUES ('$path','$path','$extension','$filesize')");
      $_SESSION["Success"]='Upload success';
      header("Location:index.php"); /* Redirect browser */
      exit();
    } else {
      $_SESSION["err"]=$text;
      header("Location:index.php"); /* Redirect browser */
      exit();
    }
  }
}

?>

下载.php

<?php
// Include config file
require_once 'config.php';


$id=$_GET["id"];
$sql = "select * from images where id=$id "; // 1
$res = $conn->query($sql);
while($row = $res->fetch_assoc())
{ 
    $image = $row['image'];
    $name = $row['name'];
    $type = $row['filetype'];
    $size =  $row['size'];
}

header("Content-type: ".$type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary"); 
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".$size);


echo $image;
exit();

?>

您的数据库屏幕截图显示您的图像块仅为14字节。这只是文件名:

 $conn->query("INSERT INTO images (image, name,filetype,size) 
               VALUES ('$path','$path','$extension','$filesize')");
 $fileData = file_get_contents($target_file);
 $conn->query("INSERT INTO images (image, name,filetype,size) 
               VALUES ('$fileData','$path','$extension','$filesize')");
这是你的问题。您正在将文件名作为文件数据插入

只需将
$path
的第一个实例替换为文件内容:

 $conn->query("INSERT INTO images (image, name,filetype,size) 
               VALUES ('$path','$path','$extension','$filesize')");
 $fileData = file_get_contents($target_file);
 $conn->query("INSERT INTO images (image, name,filetype,size) 
               VALUES ('$fileData','$path','$extension','$filesize')");

停止在您的
标题中使用
内容长度
,在使用PHP下载文件时会遇到很多问题。只是不要使用它,让浏览器接受尽可能多的服务器提供的数据

问题在于PHP在某些系统上读取文件大小的方式,并将该数据传递到
头文件
,但头文件接受的文件大小可能稍大一些(~5%),这是因为不同的程序读取磁盘空间的方式不同,也可能是因为服务器自动压缩传输中的文件(gzip)

(从web服务器/浏览器的角度来看,
blob
数据是一个文件)


同样,您的内容类型标题也不正确

 header("Content-type: ".$type); // wrong $type. 
应该是可识别的MIME类型,如image/jpeg,因此
$extension=end($array)应替换为以下内容:


$path
本质上是
time()
这是一个路径(因此是14字节),为什么要将其存储为blob?
$image
基本上是文件名(这是存储在
image
字段中的内容)。您需要使用路径从保存文件的文件系统中读取该文件,并将其内容发送到客户端。此外,您的内容类型不准确,您需要获得正确的mime类型,而不仅仅是扩展名。我想你把php代码的上传和下载搞混了,不管你从哪里下载。上载代码用于将数据保存到文件系统并链接到文件系统,而下载代码用于从数据库中获取图像数据。如果
-1
海报删除了标记或解释了为什么应保留标记,我们将不胜感激?我已经更新了我的答案,所以觉得它不再需要-1。谢谢你的回答。当我尝试这样做时,它会将除了图像之外的所有内容上传到我的数据库中。你的代码有很多问题。你最好这么做。我应该把这些加在哪里?那么错误会出现在哪里呢?@in2d中,您的代码有太多的错误(坦率地说,这个答案也是如此)。我会彻底抛弃它,从头开始。