Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Maintain the original file name

edited July 2011 in Vanilla 2.0 - 2.8
Hi, after uploading any file the filename will change to a random value. I think this is done to eliminate name conflicts but I would like to maintain the original file name so when the user downloads the file he will get something meaningful.
Is this file renaming done by the plugin or php? and can it be changed? and how.
Thanks
Tagged:

Best Answer

  • Options
    daduptadadupta New
    Answer ✓
    I had the same problem and after some tweaking, i got it to work!
    navigate to vanilla\Plugin\FileUpload\class.fileupload.plugin.php
    at line 676 replace the following code

    "
    $SaveFilename = md5(microtime()).'.'.strtolower($Extension);
    $SaveFilename = '/FileUpload/'.substr($SaveFilename, 0, 2).'/'.substr($SaveFilename, 2);
    "

    with....

    "
    $SaveFilename = $FileName;
    $SaveFilename = '/FileUpload/'.$SaveFilename;
    "

    Enjoy

Answers

  • Options
    daduptadadupta New
    Answer ✓
    I had the same problem and after some tweaking, i got it to work!
    navigate to vanilla\Plugin\FileUpload\class.fileupload.plugin.php
    at line 676 replace the following code

    "
    $SaveFilename = md5(microtime()).'.'.strtolower($Extension);
    $SaveFilename = '/FileUpload/'.substr($SaveFilename, 0, 2).'/'.substr($SaveFilename, 2);
    "

    with....

    "
    $SaveFilename = $FileName;
    $SaveFilename = '/FileUpload/'.$SaveFilename;
    "

    Enjoy
  • Options
    edited July 2011
    thanks dadupta, that did it.

    actually you don't need two line, replacing those two lines with this one line should do it:

    $SaveFilename = '/FileUpload/'.$FileName;
  • Options
    edited July 2011
    I tried to find a way to use the discussion title as a folder name, kinda to arrange the files into groups also to make finding things easy for site maintainers and to minimize file name conflicts.

    So i tried using:
    $dTitle = $Sender->EventArguments['Discussion']->Name;

    and

    $dTitle = $Sender->Discussion->Name

    with:
    $SaveFilename = '/FileUpload/'.$dTitle.'/'.$FileName;

    but non of the two worked which i think for two reasons:

    1- i don't know what i am doing hehehe.
    2- i think at the time of uploading files there is no title stored yet, so both ways i used will return empty string!

    So any help with this?

    * Update:
    I think using the title at this stage is not wise even if it possible, since the user can change it before publishing the final post. So is there something else that can be used and is unique to every discussion? like a DiscussionID, but not sure if it is already generate at that stage or not.
  • Options
    I don't really know what I am doing either. But maybe you could split into folders by User? or Date?

    I am not the best with PHP and I have tried
    $SaveFileName = '/FileUpload/'.$UserID.'/'.$FileName;
    Which did not work

    I am not sure how to implement the date.
  • Options
    Okay so I got the date thingo working. I did this:

    $Time = getdate()
    $SaveFilename = '/FileUpload/'.$Time[year].'/'.$Time[mon].'/'.$Time[mday].'/'.$Time[hours].'/'.$FileName;

    of course you can take out the or add in more depending on the amount of traffic your website gets (and hence the likeliness for a file name clash)

    Hope that helps
  • Options
    Thanks for the updates,

    the date is a good idea, maybe using just year and day will help a bit. But I am looking for a way to groups all attachments of one discussion together.

    So date will not work really well, since later updates or comments will be places in new folder.
  • Options
    Mmm,. Thinking about this, the only way I can see how to do it would be to post the discussion, then edit your post attaching the file (as the discussion now has a set name).

    Again I am not sure how you would implement this as I have never actually learned PHP.
  • Options
    I don't think it is a PHP problem we are having here, but rather a Vanilla thing. We need help from someone who knows about Vanilla code so he/she can tells us what can be used to identify a new and unsaved thread.
  • Options
    edited November 2011
    my latest update on this:

    replace:
    $SaveFilename = md5(microtime()).'.'.strtolower($Extension); $SaveFilename = '/FileUpload/'.substr($SaveFilename, 0, 2).'/'.substr($SaveFilename, 2);

    with this:
    // Build filename $OnlyFileName = strtolower($FileNameParts['filename']); $FilePath = DS . 'FileUpload' . DS . date('Y-m-d') . DS; $Cnt = 0; if (file_exists('uploads' . DS . $FilePath . $OnlyFileName . '.' . $Extension)) { $Cnt = 1; while (file_exists('uploads' . DS . $FilePath . $OnlyFileName . '_' . $Cnt . '.' . $Extension)) { $Cnt = $Cnt + 1; } } if ($Cnt == 0) { $SaveFilename = $FilePath . $OnlyFileName . '.' . $Extension; } else { $SaveFilename = $FilePath . $OnlyFileName . '_' . $Cnt . '.' . $Extension; }

    Will upload the files into a folder with a name that represent the date it was created, e.g. 2011-11-29, if the folder doesn't exist it will be created (i added this to have some kind of organized folders and not have many files from so many posts all at the same level).
    Also if a file with the same name exists it will add and incrementing counter to the file name, e.g. foo.txt foo_1.txt foo_2.txt ....etc
  • Options
    TimTim Operations Vanilla Staff
    Instead of any of these changes, you should just set this in your config:

    $Configuration['Plugins']['FileUpload']['UseDownloadUrl'] = TRUE;

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Options
    Instead of any of these changes, you should just set this in your config:

    $Configuration['Plugins']['FileUpload']['UseDownloadUrl'] = TRUE;
    That could of helped a little earlier :P does it have the auto increment in case of similar file names though?
  • Options
    TimTim Operations Vanilla Staff
    $SaveFilename = md5(microtime()).'.'.strtolower($Extension);
    This handles the similar filenames. Files are still stored hashed on the server, just served back with their original names. Using magic.

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Options

    @Tim said:
    Instead of any of these changes, you should just set this in your config:

    $Configuration['Plugins']['FileUpload']['UseDownloadUrl'] = TRUE;

    Ok I set that, and now for my custom file type (.audulus), when I click on the file the downloaded name is always default.audulus, instead of the actual file name. Seems to work fine for images and zips.

  • Options

    I'm sure there's a better way, but so far I've been able to get this working while keeping the hashed file scheme. In library/core/class.filesystem.php around line 353, comment out the lines like this:

    if ($ServeMode == 'inline')
         header('Content-Disposition: inline; filename="'.$Name.'"');
    //else
    //  header('Content-Disposition: attachment; filename="'.$Name.'"');
    

    I don't know how important it is to keep this header, but it seems to solve this issue where $Name is coming up empty for some reason. Tim are you referring to mod_mime_magic?

Sign In or Register to comment.