Menu

#486 Enforce unique filename on media upload

phpGedView
open
5
2006-06-20
2005-11-08
Xena
No

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.

Discussion

  • Laie Techie

    Laie Techie - 2005-11-09

    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.

     
  • Xena

    Xena - 2005-11-13

    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;
    }
    ?>

     
  • Xena

    Xena - 2005-11-13

    Modified edit_interface.php, search for [DTL]

     
  • Xena

    Xena - 2005-11-13

    Logged In: YES
    user_id=1142706

    Added the functions_edit.php file with the mediacount
    function added at the end.

     
  • Xena

    Xena - 2005-11-13
     
  • KosherJava

    KosherJava - 2006-06-20
    • assigned_to: nobody --> canajun2eh
     
  • KosherJava

    KosherJava - 2006-06-20

    Logged In: YES
    user_id=634811

    Gerry,
    Has this been implemented as part of your media work?

     
  • KosherJava

    KosherJava - 2007-04-19

    Logged In: YES
    user_id=634811
    Originator: NO

    Gerry,
    Has this been implemented as part of your media work?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.