从sql数据库下载php后我得到一个空的excel文件

时间:2013-12-14 16:09:14

标签: php mysql sql excel

我有一些问题。我有一个mySQL数据库,我想存储一些peronal文件。我知道这不是处理它的最佳方式,但它们只是一些excel文件,因此我认为这是一个很大的交易。但是我只使用PHP下载空文件或excel文件。这就是我的SQL表中的内容:

['name'] = The name of the file;
['mime'] = The type of file (application/vnd.ms-excel);
['size'] = The size of the data;
['data'] = And the data itself;

此外,还有一个用户ID可供谁查看该文件。

这是我的下载文件:

if(isset($_GET['id']))
{
  $id=intval($_GET['id']);
  if($id <= 0)
  {
    die('Het opgegeven ID is niet correct');
  }
  else
  {
    $sql_facturen=mysqli_query($con,"SELECT mime, name, data FROM facturen WHERE id='" . $id . "'");
    echo '<p> doing query </p>';
    if($sql_facturen)
    {
      if(mysqli_num_rows($sql_facturen) == 1)
      {
        $row_facturen=mysqli_fetch_assoc($sql_facturen);
        header("Content-Length: " . $row_facturen['size']);
        header("Content-Type: " . $row_facturen['mime']);
        header("Content-Disposition: attachment; filename=" .basename( $row_facturen['name']));

        readfile($row_facturen['data']);
      }
      else
      {
        echo '<p>Geen factuur met dat id aanwezig</p>';
      }
    }
  }
}

我尝试过在网络上找到的多个标头配置。我也试过echo而不是readfile。然而,不应该在其中的文本。任何sugestions?我使用谷歌浏览器。

编辑: 这是我的上传文件:

if(isset($_FILES['uploaded_file']))
                if($_FILES['uploaded_file']['error'] == 0) 
                {
                    $name = mysqli_real_escape_string($con, $_FILES['uploaded_file']['name']);
                    $mime = mysqli_real_escape_string($con, $_FILES['uploaded_file']['type']);
                    $data = mysqli_real_escape_string($con, ($_FILES['uploaded_file']['tmp_name']));
                    $size = mysqli_real_escape_string($con, $_FILES['uploaded_file']['size']);
            $year = $_POST['year'];
                    $user_id = $_GET['id'];

                    $sql_upload=mysqli_query($con,"INSERT INTO facturen (name, mime, size, data, created, year, user_id) VALUES ('" . $name . "','" . $mime . "','" . $size . "','" . $data . "', NOW(), '" . $year . "','" . $user_id . "')");
            if ($sql_upload)
            {
                echo 'Bestand is succesvol ingevoerd';
            }
            else
            {
                        echo 'Error uploading';
                        echo mysqli_error($con);
            }
                }

1 个答案:

答案 0 :(得分:1)

试试这个

$fileSize = strlen($row_facturen['data']);

发送标题如下:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$row_facturen['name'].'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $fileSize);
echo $row_facturen['data'];
exit();

[从这里添加,有利于用户]​​

将文件保存到数据库中:

$tmpName=$_FILES['uploaded_file']['tmp_name'];                     
$fp      = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($content);
fclose($fp);

同时将mysql字段类型(您的数据字段)指定为MEDIUMBLOB