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