Reputation: 6854
I have been looking for a solution to a problem I'm having with my code. The nearest solution to my problem was found here however that solution didn't fit my problem.
I'm listing uploaded files in PHP on my page, with each file being an to the file's location. That works fine, however I am new to PHP and am having difficulty in implementing others code.
Here's the code I'm using, if it helps:
<?php
// Directory Location
$directory = './';
// Timezone (UK BST)
date_default_timezone_set(WET);
// Define Byte Sizes
function file_size($size)
{
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 1) . $filesizename[$i] : '0 Bytes';
}
// fetches all executable files in that directory and loop over each
foreach(glob($file.'*wav') as $file) {
// List Link to Files & Date/Time
echo '<a class="files" href="'.$file.'">
<span class="time">' . date("m/d/Y H:i", filemtime($file));
// List File Name & Size
echo '</span> '.$file.'
Size: '.file_size(filesize($file)).'</a>';
}
?>
If possible I would prefer the download links to be generated in PHP for each individual file. The files are listed like this: https://i.sstatic.net/N8Lxa.png
Upvotes: 0
Views: 760
Reputation: 539
for list of files list.php e.g.
<?php
// salt for md5 check
$saltmd5 = '76t7gjbertdfv56w45sdve54etyv';
// Directory Location
$directory = './';
// Timezone (UK BST)
date_default_timezone_set(WET);
// Define Byte Sizes
function file_size($size)
{
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 1) . $filesizename[$i] : '0 Bytes';
}
// fetches all executable files in that directory and loop over each
foreach(glob($file.'*wav') as $file) {
// List Link to Files & Date/Time
$file_md5 = md5("$file$saltmd5"); // md5 file check
$file_link = "dwn.php?f=$file&c=$file_md5"; // file link
echo '<a class="files" href="'.$file_link.'">
<span class="time">' . date("m/d/Y H:i", filemtime($file));
// List File Name & Size
echo '</span> '.$file.'
Size: '.file_size(filesize($file)).'</a>';
}
?>
for download of files dwn.php e.g.
<?php
// salt for md5 check
$saltmd5 = '76t7gjbertdfv56w45sdve54etyv';
$file_md5 = $_GET['c']; // md5 file check
$filename = $_GET['f']; // the name of the file that is downloaded
if($file_md5==md5("$filename$saltmd5")){
// Directory Location
$directory = './';
$size = filesize($directory . $filename) ;
header("Content-Type: application/force-download; name=\"". $filename ."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size ."");
header("Content-Disposition: attachment; filename=\"". $filename ."\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo (readfile($directory . $filename));
}
else
{
die('no existo'); //bad file
}
?>
Upvotes: 0
Reputation: 57114
You should use the code of the mentioned SO answer in the following way:
change the link you display to the following which direct the user to download.php and hands over the information which file to download:
echo '<a class="files" href="download.php?file='.$file.'"> ...
Then add an download.php with the content of the SO answer but using
$filename = $_GET['file'];
to get the given filename out of the url.
Additionally you have to change header('Content-Type: application/pdf');
to whatever filetype you want to be downloaded.
Upvotes: 1