【问题标题】:Rename file before downloading PHP在下载 PHP 之前重命名文件
【发布时间】:2012-05-25 13:28:20
【问题描述】:

我目前正在制作一个文件托管网站。在用户下载文件之前重命名文件时遇到问题。上传文件时,原始名称保存在 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);
        }
?>

【问题讨论】:

  • 我希望 $fileid2$filename$filesize 在存储到数据库之前进行清理,以防止 HTTP 标头注入。
  • 有什么问题?我没有看到将文件发送到客户端的任何代码,但除此之外没有什么特别突出的......
  • 您在header 中使用$fileid2。如果我理解正确,您希望这是一个不同的名称,对吗?
  • 是的,我想把名字改成$filename
  • $info['name']不是原文件名吗?

标签: php html


【解决方案1】:

我认为你的“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);

我没有对此进行测试,所以希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2014-03-04
    • 2012-02-13
    • 2023-03-14
    • 2012-01-04
    • 2018-12-22
    • 2012-05-04
    相关资源
    最近更新 更多