实例化 Varien_File_Uploader 来实现文件/图片上传

if(isset($_FILES['logo']['name']) && $_FILES['logo']['name'] != ''){
    try{
        $uploader= newVarien_File_Uploader('logo');
        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png','csv'));
        $uploader->setAllowRenameFiles(true);
        $uploader->setFilesDispersion(false);
        $path     = Mage::getBaseDir('media') . DS . 'logo'. DS;
        $logoName= uniqid() . '_'. $_FILES['logo']['name'];
        $uploader->save($path, $logoName);
        /* get uploaded file name after saving*/
        $uploader->getUploadedFileName()
    }catch(Exception $e){
        //...
    }
}

实例化 Varien_Image 来实现缩略图

public function getResizedImage($resizeWidth, $resizeHeight, $quality= 100)
{
    $path       =  Mage::getBaseDir('media') . DS . 'logo'. DS;
    $fileName   =  $this->getFileName();
    $resizePath=  Mage::getBaseDir('media') . DS . 'logo'. DS . 'resize'. DS;

    $imageObj= newVarien_Image($path. $fileName);
    $imageObj->constrainOnly(true);
    $imageObj->keepAspectRatio(true);
    $imageObj->keepFrame(true);
    $imageObj->keepTransparency(true);
    $imageObj->quality(quality);
    $imageObj->backgroundColor(array(255,255,255));
    $imageObj->resize($resizeWidth, $resizeHeight);
    $imageObj->save($resizePath. $fileName);   
}

实例化 Varien_Io_File 来实现从数据库中导出到CSV文件中

public function exportToCsv()
{
    $path= Mage::getBaseDir() . DS . 'csv';
    $name= 'sample.csv';
    $file= $path. DS . $name;

    self::createDir($path);

    $csvData= $this->_getCsvDataFromDb($localecode)->getData();

    $io= newVarien_Io_File();
    $io->setAllowCreateFolders(true);
    $io->open(array('path'=> $path));
    $io->streamOpen($file);
    $io->streamLock(true);
    foreach($csvDataas$data) {
        $io->streamWriteCsv($data);
    }
    $io->streamUnlock();
    $io->streamClose();
}

private function _getCsvDataFromDb($localecode)
{
    $collection= Mage::getModel('csv/csv')->getcollection();
    $collection->addFieldToFilter('locale', $localecode);
    $collection->getSelect()->setPart(
            'columns',
            array(
                  array('main_table', 'csv_key', 'csv_key'),
                  array('main_table', 'csv_value', 'csv_value')
            )
    )->order('csv_key ASC');

    return $this;
}

public static function createDir($filePath)
{
    if(!file_exists($filePath)) {
        returnmkdir($filePath, 0777, true);
    }
    return true;
}

results matching ""

    No results matching ""