Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Unique upload filenames
  • I'm not much of a coder but perhaps someone can help me on this.

    I need the filename of each upload to be appended with a time reference in order to create both a unique filename for each upload but also a way to reference when it was done.

    This is to prevent different users from overwriting other users files by using the same filename such as picture1.jpg for example. Ideally, mypicture.jpeg would perhaps become mypicture1040.jpeg

    I'm guessing I would modify uploadify.php but not sure what to do.

    Any help really appreciated.
  • What I often do is to name it with a date stamp - down to the second.

    I'll grab the variables with the date() function, then do something like this:

    $newfile1="$thisyear$thismonth$thisday$thishour$thisminute$thissecond$extension";

    copy($tmpname, "$imagedir/$newfile1");

    Hope that helps. This board is getting so spammy, it's almost not worth it to try to use it since the creators of Uploadify seem to be busy with other stuff. Maybe they're making something even cooler...
  • Thanks Harleycodr, i'll give that a go and post the results (if I get it to work) here for others to use.

    I've used phpbb board software and suffered the same spam assault, ended up changing to SMF rather than spend days deleting spam registrations !
  • Hey, You're in luck cuz I need unique file names too, so I wrote something to share since uploadify is a life saver at getting file uploads with progress bars to work. I tried to get uber uploader to work on dreamhost and eventually gave up, and I'm glad I did cuz uploadify is the easiest to implement uploader. Wish it had built in options for unique names... and since I wrote this it does :-D This is taking the basic format from the pear http upload for sequential names. It checks to see if the file already exists. If it does it adds [1] to it, or [2] etc.
    i.e.
    file.jpg
    file[1].jpg
    file[2].jpg
    ...
    Here is the code ( use this in place of the code in the uploadify.php file )

    if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

    //$fileTypes = str_replace('*.','',$_REQUEST['fileext']);
    // $fileTypes = str_replace(';','|',$fileTypes);
    // $typesArray = split('\|',$fileTypes);
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    $fileExt = '.'.$fileParts['extension'];

    // if (in_array($fileParts['extension'],$typesArray)) {
    // Uncomment the following line if you want to make the directory if it doesn't exist
    // mkdir(str_replace('//','/',$targetPath), 0755, true);
    $tfn = 0;
    while(is_file($targetFile)) {
    $targetFile = str_ireplace($fileExt, '', $targetFile);
    $targetFile = str_ireplace('['.$tfn.']', '', $targetFile);
    $tfn++;
    $targetFile = $targetFile.'['.$tfn.']'.$fileExt;
    }
    move_uploaded_file($tempFile,$targetFile);
    echo \"1\";
    // } else {
    // echo 'Invalid file type.';
    // }
    }


    Good Luck!
  • What a star, I'll give this a go as soon as I get a break from Prestashop installs and post how I got on. If it goes well, there might be some value here for creating an upload module from this script for Prestashop allowing users to upload large images in a nicer way for shops such as T-shirt printers or custom merchandise manufacturers.
  • Very useful tmickey! One question though - is it possibly to access the new (unique) file name on the upload form using fileObj.name? When I try, it still only gives the name of the file that was uploaded, but not the new, unique file name. Thanks!
  • Here was my solution

    /* Uploadify v2.1.4 */
    if (!empty($_FILES)) {

    $name = reset(explode(".",$_FILES['Filedata']['name']));
    $ext = end(explode(".",$_FILES['Filedata']['name']));
    $filename = date('h:i:s A', strtotime('-1 hour'))." - ".$name.".".$ext;

    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . date('Y.m-d') . '/';
    $targetFile = str_replace('//','/',$targetPath) . $filename;


    if (!file_exists ( $targetPath )) {
    mkdir(str_replace('//','/',$targetPath), 0755, true);
    move_uploaded_file($tempFile,$targetFile);
    echo $filename;

    } else {
    move_uploaded_file($tempFile,$targetFile);
    echo $filename;

    }} else {
    echo 'Invalid file type.';
    }

    ?>


    It saves files in a directory with today's date, and the file names are appended with the timestamp:

    Uploads/2011.08-12/03:11:58 PM - Work_Request.pdf

    Oh, and I put the strtotime('-1 hour') because our server is 1 hour ahead of us.

    I'm still trying to figure out how to feed the information back to my form so the submitted email has the new unique filename.
  • That is Fantastic...

    Is it possible to have a input box that would require the person uploading the file to type their name so that the folder that saves the file would have their name (last or first or both?)
  • Which version are you using? Depending on the version, you can create an optional callback that uses any data that is printed in the uploadify.php file. In version 2.x, you can use the onComplete callback...

    $('#fileUpload').uploadify({
    ...
    'onComplete' : function(event, ID, fileObj, response, data) {
    $('#formFieldID').val(response);
    }
    });


    In version 3.x, you can use the onUploadSuccess callback...

    $('#fileUpload').uploadify({
    ...
    'onUploadSuccess' : function(file, data, response) {
    $('#formFieldID').val(response);
    }
    });
  • I'm using the latest version of 2. What would be the full code that would allow the person to input a name, then upload the files, and then have those files saved in a subfolder using the name that they entered?

    Thanks,

    Jonathan
  • Sweet! I finally found a couple threads with the solutions I needed. For my particular needs, my files have to be unique from each other, and are renamed by the uploadify.php script. I figured out how to output the results, but errors would show up if the directory(ies) being created already existed. So I wrote a if (!file_exists) statement.

    uploadify.php
    <?php
    /* Uploadify v2.1.4 */
    if (!empty($_FILES)) {

    $name = reset(explode(".",$_FILES['Filedata']['name']));
    $ext = end(explode(".",$_FILES['Filedata']['name']));
    $filename = date('h:i:s A', strtotime('-1 hour'))." - ".$name.".".$ext;

    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . date('Y.m-d') . '/';
    $targetFile = str_replace('//','/',$targetPath) . $filename;


    if (!file_exists ( $targetPath )) {
    mkdir(str_replace('//','/',$targetPath), 0755, true);
    move_uploaded_file($tempFile,$targetFile);
    echo $filename;

    } else {
    move_uploaded_file($tempFile,$targetFile);
    echo $filename;

    }} else {
    echo 'Invalid file type.';
    }

    ?>


    Next I needed multiple files to show up in the response, but only one would show up. Below is a fix I found on another thread along with the hidden input that carries the information through to the post.

    My uploadify settings
    $(function() {
    $('#file_upload').uploadify({
    'uploader' : 'incl/uploadify/uploadify.swf',
    'script' : 'incl/uploadify/uploadify.php',
    'cancelImg' : 'incl/uploadify/cancel.png',
    'folder' : 'uploads/',
    'method' : 'post',
    'buttonText' : 'Browse',
    'displayData' : 'percentage',
    'fileExt' : '*.pdf',
    'fileDesc' : 'Acrobat File (PDF)',
    'queueSizeLimit': 10,
    'sizeLimit' : 104857600,
    'multi' : true,
    'auto' : true,
    'removeCompleted': false,
    'onAllComplete' : function(event, queueID, fileObj, response, data) {
    $('#status-message').text(data.filesUploaded + ' files uploaded, ' + data.errors + ' errors.');
    },
    'onComplete': function(event, queueID, fileObj, response, data) {
    $('#uploaded_files').val($('#uploaded_files').val()+'<br/>'+response);
    }

    });
    });


    Line for hidden input somewhere in your form page

    <input name="uploaded_files" id="uploaded_files" class="inputbox" type="hidden" />




  • beyerjonathan said: beyerjonathan August 12 Permalink Flag
    I'm using the latest version of 2. What would be the full code that would allow the person to input a name, then upload the files, and then have those files saved in a subfolder using the name that they entered?

    Thanks,

    Jonathan


    Hey Jonathan,

    Originally this was what I wanted to do as our uploads are coming from students, and I wanted to organize all the uploads by their names. I'm not much of a programer, but as far as I can tell, unless the user hits a submit button, the information stored in a field can't be passed along to the upload.php file without some type of ajax/flash implementation, which is far beyond me. Since the resulting email of my form submits a unique filename, and the files are organized into dated folders, I figured this would work enough for our needs.

    -Matt