file download in mysql

1

I have files in my database that I would like to make available for download. These files are of several extensions; .pdf, .txt, doc, .xlxs and etc;

I searched in a lot of places but did not find anything detailed (for beginners).

I want to make a list of the files you have in my bank and generate a download link.

    
asked by anonymous 22.07.2015 / 17:31

2 answers

1

Public directory

The folder ./upload being "public" then we will be able to access your files, the files being static simply list the mysql data and create a .htacces in the ./upload folder to force the files to download in this folder, for example:

CREATE TABLE IF NOT EXISTS 'arquivos' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'path' varchar(800) NOT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB;

The SELECT would look something like:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

$query = 'SELECT path FROM 'arquivos'';

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        echo '<a href="', $row['path'],
             '">Download de ', basename($row['path']),
             '</a>';
    }

    $result->close();
}

$mysqli->close();

.htaccess should be in the same folder as the files:

<FilesMatch "\.(?i:mov|mp3|jpg|pdf)$"> #altere nesta linha os tipos de arquivos
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

However if you can not access or use apache you can try the download="" attribute, it would look something like:

$query = 'SELECT path FROM 'arquivos'';

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $nome = basename($row['path']);

        echo '<a download="', $nome,'" href="', $row['path'],
             '">Download de ', $nome,
             '</a>';
    }

    $result->close();
}

$mysqli->close();

Private Directory

If you do not want to allow direct access to the directory or do not have access because it is not inside the server folder, you may need to create a php or a route with .htaccess,

Assuming (hypothetical) that the server files are in /etc/var/www (or in windows c:/www ) and the files to download in /home/user/upload (or in windows c:/upload ) then you should create a .php file as well :

$query = 'SELECT path FROM 'arquivos'';

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $nome = basename($row['path']);

        echo '<a href="download.php?file=', urlencode($nome),
             '">Download de ', $nome,
             '</a>';
    }

    $result->close();
}

$mysqli->close();

and the download.php file should look something like:

  

Using snippet of this response link

<?php
$nome = isset($_GET['file']) ? $_GET['file'] : null;
$fullPath = '/home/user/upload/' . $nome;

if ($nome === null) {
    echo 'Parametro file não definido';
    exit;
} else if (false === is_file($fullPath)) {
    echo 'Arquivo não encontrado';
    exit;
}

function mimeType($file) {
    $mimetype = false;

    if (class_exists('finfo')) {//PHP5.4+
        $finfo     = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype  = finfo_file($finfo, $file);
        finfo_close($finfo);
    } else if (function_exists('mime_content_type')) {//php5.3 ou inferiror
        $mimetype = mime_content_type($file);
    }

    return $mimetype;
}

$mime = mimeType($fullPath);

if ($mime === false) {
    echo 'Não foi possível detectar o tipo de arquivo';
    exit;
}

header('Content-type: ' . $mime);

//Seta o tamanho do arquivo
header('Content-length: ' . filesize($fullPath));

//Força o download
header('Content-Disposition: attachment; filename=' . $nome);

//Este header é necessário
header('Content-Transfer-Encoding: binary');

echo file_get_contents($fullPath, FILE_BINARY);
    
23.07.2015 / 00:08
0

Technically it's simple, you just have to give a request in the DB and do a while writing:

<a href="<? echo $row[link_conteudo']?>"><? echo $row[titulo']?></a>
    
22.07.2015 / 23:24