Magento Custom Options – Set File Upload Name

custom-optionsfile upload

Custom Option – File Upload:

When uploading a file on a Custom Option of a Product it ends up at:

\media\custom_options\quote*

When you upload a file.jpg, it ends up being renamed into 123kjhkjhkjhdsk.jpg.

What I need:

I need it to be saved in Original Name.

What I have done so far:

Now, I was able to modify \lib\Varien\File\Uploader.php
https://github.com/varinen/solvingmagento_1.7.0/blob/master/lib/Varien/File/Uploader.php#L532

static public function getDispretionPath($fileName)
{
    $dispretionPath = '/orders';

    return $dispretionPath;
}

to store the files into a different folder.

But I cant find anywhere where the filename is being changed.

Any ideas?

PS: reason for Original Filename is because files uploaded need to be avaliable for download through FTP. So having original name and order # as a folder will solve current requirement.

Best Answer

First of all I want to say that you shouldn't use the real name of the file. What if 2 customers upload a file with the same name? The first one will get overwritten.
I suggest you use something like original_file_name_SomeHashHERe.png.
Also don't change the getDispretionPath method from the uploader class.
For 2 reasons: 1. You should never edit the core code. 2. That method is used by multple actions. If you change it it will be changed for all the other actions. You don't want that.

Now a possible solution. The file name is changed in the method Mage_Catalog_Model_Product_Option_Type_File::_validateUploadedFile.
More specific on this line:

$fileHash = md5(file_get_contents($fileInfo['tmp_name']));  

Override the method and change that line.
Also the same method can solve you your dispersion path problem.
The line above the one that changes the file name is

$filePath = $dispersion;

Change $dispersion to what ever you need.

Related Topic