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.
Maintain the original file name
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
Is this file renaming done by the plugin or php? and can it be changed? and how.
Thanks
Tagged:
0
Best Answer
-
dadupta NewI 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-1
Answers
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
actually you don't need two line, replacing those two lines with this one line should do it:
$SaveFilename = '/FileUpload/'.$FileName;
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.
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.
$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
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.
Again I am not sure how you would implement this as I have never actually learned PHP.
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
$Configuration['Plugins']['FileUpload']['UseDownloadUrl'] = TRUE;
Vanilla Forums COO [GitHub, Twitter, About.me]
$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]
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.
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:
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?