在下载PHP之前重命名文件

时间:2012-05-17 23:14:50

标签: php html

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

1 个答案:

答案 0 :(得分: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);

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