Generate the URL:
/**
* For file and image field, get the file URL
*
* @see Mage_Customer_Model_Attribute_Data_File
* @return string
*/
public function getFileUrl()
{
return $this->getUrl('extendedcustomer/account/viewfile', array(
'file' => Mage::helper('core')->urlEncode($this->getValue()),
));
}
Controller:
class Scicom_ExtendedCustomer_AccountController extends Mage_Core_Controller_Front_Action
{
/**
* @see Mage_Adminhtml_CustomerController
*/
public function viewfileAction()
{
$file = null;
$plain = false;
if ($this->getRequest()->getParam('file')) {
// download file
$file = Mage::helper('core')->urlDecode($this->getRequest()->getParam('file'));
} else if ($this->getRequest()->getParam('image')) {
// show plain image
$file = Mage::helper('core')->urlDecode($this->getRequest()->getParam('image'));
$plain = true;
} else {
return $this->norouteAction();
}
$path = Mage::getBaseDir('media') . DS . 'customer';
$ioFile = new Varien_Io_File();
$ioFile->open(array('path' => $path));
$fileName = $ioFile->getCleanPath($path . $file);
$path = $ioFile->getCleanPath($path);
if ((!$ioFile->fileExists($fileName) || strpos($fileName, $path) !== 0)
&& !Mage::helper('core/file_storage')->processStorageFile(str_replace('/', DS, $fileName))
) {
return $this->norouteAction();
}
if ($plain) {
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
switch (strtolower($extension)) {
case 'gif':
$contentType = 'image/gif';
break;
case 'jpg':
$contentType = 'image/jpeg';
break;
case 'png':
$contentType = 'image/png';
break;
default:
$contentType = 'application/octet-stream';
break;
}
$ioFile->streamOpen($fileName, 'r');
$contentLength = $ioFile->streamStat('size');
$contentModify = $ioFile->streamStat('mtime');
$this->getResponse()
->setHttpResponseCode(200)
->setHeader('Pragma', 'public', true)
->setHeader('Content-type', $contentType, true)
->setHeader('Content-Length', $contentLength)
->setHeader('Last-Modified', date('r', $contentModify))
->clearBody();
$this->getResponse()->sendHeaders();
while (false !== ($buffer = $ioFile->streamRead())) {
echo $buffer;
}
} else {
$name = pathinfo($fileName, PATHINFO_BASENAME);
$this->_prepareDownloadResponse($name, array(
'type' => 'filename',
'value' => $fileName
));
}
exit();
}
}
Mage_Adminhtml_Model_System_Config_Backend_File
in app\code\core\Mage\Adminhtml\Model\System\Config\Backend\File.php
$uploadDir = $this->_getUploadDir();
try {
$file = array();
$tmpName = $_FILES['groups']['tmp_name'];
$file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value'];
$name = $_FILES['groups']['name'];
$file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value'];
$uploader = new Mage_Core_Model_File_Uploader($file);
$uploader->setAllowedExtensions($this->_getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$this->addValidators( $uploader );
$result = $uploader->save($uploadDir);
} catch (Exception $e) {
Mage::throwException($e->getMessage());
return $this;
}
Validator:
/**
* Add validators for uploading
*
* @param Mage_Core_Model_File_Uploader $uploader
*/
protected function addValidators(Mage_Core_Model_File_Uploader $uploader)
{
$uploader->addValidateCallback('size', $this, 'validateMaxSize');
}
/**
* Validation callback for checking max file size
*
* @param string $filePath Path to temporary uploaded file
* @throws Mage_Core_Exception
*/
public function validateMaxSize($filePath)
{
if ($this->_maxFileSize > 0 && filesize($filePath) > ($this->_maxFileSize * 1024)) {
throw Mage::exception('Mage_Core', Mage::helper('adminhtml')->__('Uploaded file is larger than %.2f kilobytes allowed by server', $this->_maxFileSize));
}
}
Posted by kiatng to OpenMage (2020-07-10 06:49)