Enforce unique filename on media upload
Brought to you by:
canajun2eh,
yalnifj
My mom uses a scanner to scan and then upload media
files and it appears to create files named "scan.jpg".
She ends up replacing the existing file on upload.
It would be nice if phpgedview could prompt on
duplicate filenames and/or generate a unique ID to tack
on to the name of uploaded files if a duplicate
filename already exists.
Logged In: YES
user_id=1278885
I think it would be easy to append and/or increment numbers
after the filename (scan.jpg scan2.jpg scan3.jpg, etc).
Like you pointed out, the issue is does the person want to
replace the existing file or not.
Logged In: YES
user_id=1142706
I made this work through the edit interface by (1) creating
the following function in functions_edit.php and (2)
modifying edit_interface.php -- added 2 lines and modified
5. See the attached file and search for [DTL]
/**
* mediacount
*
* Author = David Lutz [DTL]
*
* This function generates a unique # for adding to media
filenames to ensure existing files are not overwritten
* Reads last number from file, increments number, writes to
file
* On each file uploaded in edit_interface, the following
two lines call mediacount to ensure a unique filename
$file_name_ext =
explode(".",basename($upload['name'])); // separates
filename and extension
$unique_filename = $file_name_ext[0] . "_pgv" .
mediacount() . "." . $file_name_ext[1];
* Result is that "picture.jpg" becomes picture_pgvx.jpg",
where x = an ever increasing # generated by mediacount()
*/
function mediacount() {
global $GEDCOM, $DEBUG;
global $INDEX_DIRECTORY;
// Check if file_get_contents() is supported
if (!function_exists("file_get_contents")) {
function file_get_contents($filename) {
$filestring = "";
$file = @fopen($filename, "r");
if ($file) {
while (!feof($file)) $filestring .= fread($file, 1024);
fclose($file);
}
return $filestring;
}
}
$PGV_MEDIACOUNTER_FILENAME =
$INDEX_DIRECTORY.$GEDCOM."pgv_mediacount.txt";
// if counter file doesn't exist create it
if(!file_exists($PGV_MEDIACOUNTER_FILENAME))
{
$fp=fopen($PGV_MEDIACOUNTER_FILENAME,"w");
fputs($fp,"0");
fclose($fp);
$mediacount = 0;
}
else // file exists, so increment media count
{
$mediacount = file_get_contents($PGV_MEDIACOUNTER_FILENAME);
$mediacount++;
$fp=fopen($PGV_MEDIACOUNTER_FILENAME,"r+");
fputs($fp,$mediacount);
fclose($fp);
}
return $mediacount;
}
?>
Modified edit_interface.php, search for [DTL]
Logged In: YES
user_id=1142706
Added the functions_edit.php file with the mediacount
function added at the end.
Logged In: YES
user_id=634811
Gerry,
Has this been implemented as part of your media work?
Logged In: YES
user_id=634811
Originator: NO
Gerry,
Has this been implemented as part of your media work?