2

我正在尝试为我的文件上传脚本制作自动下载页面 url 缩短器。

将数据插入数据库的部分示例:

function RandomString($length) {
    $key = "";
    $keys = array_merge(range(0,9), range('a', 'z'), range('A', 'Z'));

    for($i=0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }
    return $key;
}

try {
    $db = new PDO('mysql:host='. DB_HOST .';dbname=' . DB_DATABASE .';charset=utf8', DB_USER, DB_PASSWORD);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

    $download_code = RandomString(5);
    $query = "INSERT INTO user_uploads(User_Id, Name, File, DownloadCode) VALUES('".$USER_ID."','".$upload_name."','".$upload_file."','".$download_code."'")";
    $link = $db->prepare($query);
    if($link->execute()){
        header("location: upload-success");
        exit();
    }
    $db = null;
}catch(PDOException $e) {
    die($e->getMessage());
}

所以,假设下载代码是“KD9UI”

如何制作像这样工作的下载页面? http://domain.com/KD9UI

当访问者访问该 url 时,页面通过此下载代码从数据库中获取数据,并将文件详细信息打印给访问者。

家庭帮助会很好:)

4

1 回答 1

1

将 id 传递给上传成功页面。

像这样:

header('Location: upload-success?download_code='.$download_code);

并将 domain.com/id 重定向到 domain.com/download.php?download_code=id

像这样:

RewriteRule ^upload-success$ upload_success.php [L,NC]
/*
other rewrite rules..
this rule must be at last rule
becuse another rules may be exists too and it's domain.com/(...)
*/
RewriteRule ^(.+?)$ download.php?download_code=$1 [L,NC]
于 2013-04-02T03:28:54.190 回答