上传图片

if (isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') {
                    try {
                        $fileName       = $_FILES['image']['name'];
                        if($_FILES['image']['size'] > 2097152){
                            Mage::getSingleton('customer/session')->addError(Mage::helper('flagbit_faq')->__('Image size cannot exceed 2MB.'));
                            $this->_redirect('*/*/form');
                            return;
                        }
                        $fileExt        = strtolower(substr(strrchr($fileName, ".") ,1));
                        $fileNamewoe    = rtrim($fileName, '.'.$fileExt);
                        $fileName       = preg_replace('/\s+/', '', $fileNamewoe).'_' . time() . '.' . $fileExt;
                        $uploader       = new Varien_File_Uploader('image');
                        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $path = Mage::getBaseDir('media') . DS . 'faq';
                        if(!is_dir($path)){
                            mkdir($path, 0777, true);
                        }
                        $uploader->save($path . DS, $fileName );
                        $post['image'] = $fileName;
                    } catch (Exception $e) {
                        $error = true;
                    }
                }

你可能感兴趣的:(上传图片)