I present a small fix that might help one or the other: Custom Download names
Purpose
AppGini is able to make sure names of uploaded files (and images) are not overwritten if you disable keeping the filename.
This however turns out to be ugly when the user downloads a file again.
I present adjustments to make custom download names possible. These are pulled from another column of the table.
As this uses a different technique than AG itself uses, it could be possible (yet untested) to use this to pull content from a folder outside webroot.
Disclaimer
a. Use at your own risk.
b. make a backup of all files and your database before you start
c. Make sure to
a. read AND
b. understand the documentation in the beginning of this file AND
c. implement necessary adjustments in your application AND
d. that you make necessary adjustments in the adjustment-area below
THIS SCRIPT HAS BEEN TESTED AND THE INSTALLATION EXPLANATION HAS BEEN CHECK REPEATEDLY, BUT THERE COULD BE AN ERROR.
ALSO, YOU MAY MAKE AN ERROR IMPLEMENTING THE SCRIPT.
YOU ARE ON YOUR OWN AND IT IS SOLELY YOUR RESPONSIBILITY! NO HELP CAN BE EXPECTED, NO GUARANTEE CAN BE GIVEN!
Installation
IMPORTANT: BACKUP YOUR APPLICATION BEFORE YOU START!
General Config
ATTENTION: You will need to repeat THIS change to link.php EVERY time you regenerate your application!
10) open /link.php
In the beginning of the file you find something like this:
Code: Select all
// Upload paths
$p = [
'tbldocuments' => [
'filename_uploaded' => getUploadDir(''),
'primary key' => 'id'
],
];For each table add the following line after the table name.
Add
Code: Select all
'downloadFilename' => 'filename_original', // add the fieldname under which the file should be downloaded to the userCode: Select all
// Upload paths
$p = [
'tbldocuments' => [
'downloadFilename' => 'filename_original', // add the fieldname under which the file should be downloaded to the user
'filename_uploaded' => getUploadDir(''),
'primary key' => 'id'
],
];Code: Select all
'downloadFilename' => '', // add the fieldname under which the file should be downloaded to the userCode: Select all
getLink($t, $f, $p[$t]['primary key'], $i, $p[$t][$f]);Code: Select all
getLink($t, $f, $p[$t]['primary key'], $i, $p[$t][$f], $p[$t]['downloadFilename']);Code: Select all
function getLink($table = '', $linkField = '', $pk = '', $id = '', $path = '') {Code: Select all
function getLink($table = '', $linkField = '', $pk = '', $id = '', $path = '', $downloadFilename = '')(complete IF statement)
Code: Select all
if(preg_match('/^(http|ftp)/i', $link)) { // if the link points to an external url, don't prepend path
$path = '';
} elseif(!is_file(__DIR__ . "/{$path}{$link}")) { // if the file doesn't exist in the given path, try to find it without the path
$path = '';
}Code: Select all
// Correcting how we set the path for local files
if (preg_match('/^(http|ftp)/i', $link)) { // If the link points to an external URL, don't prepend path
$path = '';
} else {
// Construct the full path
$fullPath = __DIR__ . "/{$path}{$link}";
if (!is_file($fullPath)) { // If the file doesn't exist in the given path, try to find it without the path
exit("File not found. Fullpath: '$fullPath'");
}
}Note: include the } in the end! This search should be in the end of the file.
Code: Select all
@header("Location: $path$link");
exit;
}Code: Select all
// Assuming you want to define a filename for downloads
// try custom name
$filename = getDownloadFilename($table, $pk, $id, $downloadFilename);
// fallback
if ($filename == '') {
$filename = basename($link); // Change this if you'd like a custom name
}
sendFile($fullPath, $filename);
exit;
}
/**
* read the name for the file that should be given to download
* @param mixed $table
* @param mixed $pk
* @param mixed $id
* @param mixed $downloadFilename
* @return string
*/
function getDownloadFilename($table = '', $pk = '', $id = '', $downloadFilename = ''): string
{
$sql = "SELECT `$downloadFilename` FROM `$table` WHERE `$pk` = '$id';";
$result = sqlvalue($sql);
// Corrected comparison
if (is_null($result) || (strlen($result) == 0)) {
// Fallback
$result = '';
}
return $result;
}
function sendFile($filePath, $filename)
{
// Check if file exists
if (!file_exists($filePath)) {
exit("File not found. Path '$filePath', File: '$filename'");
}
// Set headers for file download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
// Clear output buffer and read the file
ob_clean();
flush();
readfile($filePath);
exit;
}Regenerate your application (or replace the changed link.php with the original).
Comments? Suggestions?
Olaf
