0

I'm currently making a file hosting website. I'm having a problem with renaming the file before the user downloads the file. When the file is uploaded the original name is saved in the mysql database and the file is renamed to a 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);
        }
?>
Chris
  • 25
  • 1
  • 8

1 Answers1

0

I would think your "while" loop and output would be something like this: (I also switched the $filename and $fileid2 in the headers)

    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);

I did not test this, so I hope it helps.

Adam Culp
  • 500
  • 2
  • 11