Skip to content

Instantly share code, notes, and snippets.

Last active May 25, 2018 05:50
What would you like to do?
Magento File Upload via Varien_File_Uploader
$uploader = new Varien_File_Uploader($_filename);
$uploader->setAllowRenameFiles(true); //if true, uploaded file's name will be changed, if file with the same name already exists directory. Necessary to avoid conflicts
$uploader->setFilesDispersion(false); //To have a dispersion based on the original file name (as the file option does), we will have to do it manually
$uploader->setAllowCreateFolders(true); //for creating the directory if not exists
$extension = pathinfo(strtolower($_file['name']), PATHINFO_EXTENSION);
$fileName = Mage_Core_Model_File_Uploader::getCorrectFileName($_file['name']);
$dispersion = Mage_Core_Model_File_Uploader::getDispretionPath($fileName); // We get the dispersion manually here
$fileHash = md5(file_get_contents($_file['tmp_name'])); //here the secretkey
$path = $this->getOrderTargetDir() . $dispersion;
$DestName = $fileHash . '.' . $extension;
$result = $uploader->save($path, $DestName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment