簡體   English   中英

在下載PHP之前重命名文件

[英]Rename file before downloading PHP

我目前正在制作文件托管網站。 我在用戶下載文件之前重命名文件時遇到問題。 上載文件時,原始名稱保存在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);
        }
?>

我認為您的“ 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);

我沒有對此進行測試,因此希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM