0

我目前正在制作一个文件托管网站。在用户下载文件之前重命名文件时遇到问题。上传文件时,原始名称保存在 mysql 数据库中,并将文件重命名为 id。

<?php

  require("includes/inc.php");

$id = trim(sanitize($_GET['id'])); //Receives the id of the file from the url

if($id) {
    $fileid = mysql_query("SELECT * FROM files WHERE id = '$id'");

    if (mysql_num_rows($fileid) != 1) {
        header('Location: download.php?error=invalid_file');
        exit();
    } else {
        while($info = mysql_fetch_array($fileid)) {
        $fileid2 = $info['id'];
        $filename = $info['name'];
        $ext = $info['ext'];
        $filesize = $info['size'];
        $downloads = $info['downloads'];
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header("Location: uploads/".$fileid2.".".$ext);
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header('Content-Length: ' . $filesize);
        }
?>
4

1 回答 1

0

我认为您的“while”循环和输出将是这样的:(我还在标题中切换了 $filename 和 $fileid2 )

    while($info = mysql_fetch_array($fileid)) {
            $fileid2 = $info['id'];
            $filename = $info['name'];
            $ext = $info['ext'];
            $filesize = $info['size'];
            $downloads = $info['downloads'];
    }

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$fileid2.'.'.$ext.'"');
    header('Content-Length: ' . $filesize);

readfile(uploads/".$filename.".".$ext);

我没有测试这个,所以我希望它有帮助。

于 2012-05-18T01:47:36.917 回答