Skip to content

Instantly share code, notes, and snippets.

@Niroshan3
Last active May 25, 2018 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niroshan3/964f62c0765a49deb4441ff0601523c8 to your computer and use it in GitHub Desktop.
Save Niroshan3/964f62c0765a49deb4441ff0601523c8 to your computer and use it in GitHub Desktop.
Magento File Upload via Varien_File_Uploader
<?php
$uploader = new Varien_File_Uploader($_filename);
$uploader->setAllowedExtensions(array('zip','ZIP'));
$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