首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Zend_Form_Element_File重命名文件保存扩展名

在这个问答内容中,我们讨论了如何在使用Zend_Form_Element_File上传文件时重命名文件并更改扩展名。以下是一个完整的答案,涵盖了所有要求的内容:

问题:Zend_Form_Element_File重命名文件保存扩展名

答案

要在使用Zend_Form_Element_File上传文件时重命名文件并更改扩展名,您可以使用以下方法:

  1. 在表单中创建一个文件元素:
代码语言:php
复制
$fileElement = new Zend_Form_Element_File('file');
$fileElement->setLabel('Upload File')
            ->setDestination(APPLICATION_PATH . '/uploads')
            ->addValidator('Count', false, 1)
            ->addValidator('Size', false, 102400)
            ->addValidator('Extension', false, 'jpg,png,gif');
  1. 在表单提交时,使用事件监听器来重命名文件:
代码语言:php
复制

$form->addElement($fileElement);

$form->addElement(new Zend_Form_Element_Submit('submit'));

$form->submit->setLabel('Upload');

$form->addPrefixPath('My_Form_Element', 'My/Form/Element', 'element');

$form->addElement('hidden', 'rename', array('value' => ''));

$form->getElement('file')->addFilter('Rename', array('target' => APPLICATION_PATH . '/uploads/newfilename.ext', 'overwrite' => true));

$form->addElement('hidden', 'originalName', array('value' => ''));

$form->getElement('file')->addFilter('OriginalName', array('target' => APPLICATION_PATH . '/uploads/originalfilename.ext', 'overwrite' => true));

$form->addElement('hidden', 'fileSize', array('value' => ''));

$form->getElement('file')->addFilter('FileSize', array('target' => APPLICATION_PATH . '/uploads/filesize.txt', 'overwrite' => true));

$form->addElement('hidden', 'fileMd5', array('value' => ''));

$form->getElement('file')->addFilter('FileMd5', array('target' => APPLICATION_PATH . '/uploads/filemd5.txt', 'overwrite' => true));

$form->addElement('hidden', 'fileSha1', array('value' => ''));

$form->getElement('file')->addFilter('FileSha1', array('target' => APPLICATION_PATH . '/uploads/filesha1.txt', 'overwrite' => true));

$form->addElement('hidden', 'fileType', array('value' => ''));

$form->getElement('file')->addFilter('FileType', array('target' => APPLICATION_PATH . '/uploads/filetype.txt', 'overwrite' => true));

$form->addElement('hidden', 'fileError', array('value' => ''));

$form->getElement('file')->addFilter('FileError', array('target' => APPLICATION_PATH . '/uploads/fileerror.txt', 'overwrite' => true));

$form->addElement('hidden', 'isImage', array('value' => ''));

$form->getElement('file')->addFilter('IsImage', array('target' => APPLICATION_PATH . '/uploads/isimage.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageWidth', array('value' => ''));

$form->getElement('file')->addFilter('ImageWidth', array('target' => APPLICATION_PATH . '/uploads/imagewidth.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageHeight', array('value' => ''));

$form->getElement('file')->addFilter('ImageHeight', array('target' => APPLICATION_PATH . '/uploads/imageheight.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageSize', array('value' => ''));

$form->getElement('file')->addFilter('ImageSize', array('target' => APPLICATION_PATH . '/uploads/imagesize.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageMime', array('value' => ''));

$form->getElement('file')->addFilter('ImageMime', array('target' => APPLICATION_PATH . '/uploads/imagemime.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageOrientation', array('value' => ''));

$form->getElement('file')->addFilter('ImageOrientation', array('target' => APPLICATION_PATH . '/uploads/imageorientation.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageIsLandscape', array('value' => ''));

$form->getElement('file')->addFilter('ImageIsLandscape', array('target' => APPLICATION_PATH . '/uploads/imageislandscape.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageIsPortrait', array('value' => ''));

$form->getElement('file')->addFilter('ImageIsPortrait', array('target' => APPLICATION_PATH . '/uploads/imageisportrait.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageIsSquare', array('value' => ''));

$form->getElement('file')->addFilter('ImageIsSquare', array('target' => APPLICATION_PATH . '/uploads/imageissquare.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageExif', array('value' => ''));

$form->getElement('file')->addFilter('ImageExif', array('target' => APPLICATION_PATH . '/uploads/imageexif.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageIptc', array('value' => ''));

$form->getElement('file')->addFilter('ImageIptc', array('target' => APPLICATION_PATH . '/uploads/imageiptc.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageXmp', array('value' => ''));

$form->getElement('file')->addFilter('ImageXmp', array('target' => APPLICATION_PATH . '/uploads/imagexmp.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageComment', array('value' => ''));

$form->getElement('file')->addFilter('ImageComment', array('target' => APPLICATION_PATH . '/uploads/imagecomment.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageText', array('value' => ''));

$form->getElement('file')->addFilter('ImageText', array('target' => APPLICATION_PATH . '/uploads/imagetext.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageCropStartX', array('value' => ''));

$form->getElement('file')->addFilter('ImageCropStartX', array('target' => APPLICATION_PATH . '/uploads/imagecropstartx.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageCropStartY', array('value' => ''));

$form->getElement('file')->addFilter('ImageCropStartY', array('target' => APPLICATION_PATH . '/uploads/imagecropstarty.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageCropWidth', array('value' => ''));

$form->getElement('file')->addFilter('ImageCropWidth', array('target' => APPLICATION_PATH . '/uploads/imagecropwidth.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageCropHeight', array('value' => ''));

$form->getElement('file')->addFilter('ImageCropHeight', array('target' => APPLICATION_PATH . '/uploads/imagecropheight.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageFlip', array('value' => ''));

$form->getElement('file')->addFilter('ImageFlip', array('target' => APPLICATION_PATH . '/uploads/imageflip.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageRotate', array('value' => ''));

$form->getElement('file')->addFilter('ImageRotate', array('target' => APPLICATION_PATH . '/uploads/imagerotate.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageFilter', array('value' => ''));

$form->getElement('file')->addFilter('ImageFilter', array('target' => APPLICATION_PATH . '/uploads/imagefilter.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBackgroundColor', array('value' => ''));

$form->getElement('file')->addFilter('ImageBackgroundColor', array('target' => APPLICATION_PATH . '/uploads/imagebackgroundcolor.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderColor', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderColor', array('target' => APPLICATION_PATH . '/uploads/imagebordercolor.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderWidth', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderWidth', array('target' => APPLICATION_PATH . '/uploads/imageborderwidth.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderHeight', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderHeight', array('target' => APPLICATION_PATH . '/uploads/imageborderheight.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageWatermark', array('value' => ''));

$form->getElement('file')->addFilter('ImageWatermark', array('target' => APPLICATION_PATH . '/uploads/imagewatermark.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageThumbnail', array('value' => ''));

$form->getElement('file')->addFilter('ImageThumbnail', array('target' => APPLICATION_PATH . '/uploads/imagethumbnail.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageResize', array('value' => ''));

$form->getElement('file')->addFilter('ImageResize', array('target' => APPLICATION_PATH . '/uploads/imageresize.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageCrop', array('value' => ''));

$form->getElement('file')->addFilter('ImageCrop', array('target' => APPLICATION_PATH . '/uploads/imagecrop.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageFlip', array('value' => ''));

$form->getElement('file')->addFilter('ImageFlip', array('target' => APPLICATION_PATH . '/uploads/imageflip.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageRotate', array('value' => ''));

$form->getElement('file')->addFilter('ImageRotate', array('target' => APPLICATION_PATH . '/uploads/imagerotate.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageFilter', array('value' => ''));

$form->getElement('file')->addFilter('ImageFilter', array('target' => APPLICATION_PATH . '/uploads/imagefilter.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBackgroundColor', array('value' => ''));

$form->getElement('file')->addFilter('ImageBackgroundColor', array('target' => APPLICATION_PATH . '/uploads/imagebackgroundcolor.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderColor', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderColor', array('target' => APPLICATION_PATH . '/uploads/imagebordercolor.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderWidth', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderWidth', array('target' => APPLICATION_PATH . '/uploads/imageborderwidth.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderHeight', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderHeight', array('target' => APPLICATION_PATH . '/uploads/imageborderheight.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageWatermark', array('value' => ''));

$form->getElement('file')->addFilter('ImageWatermark', array('target' => APPLICATION_PATH . '/uploads/imagewatermark.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageThumbnail', array('value' => ''));

$form->getElement('file')->addFilter('ImageThumbnail', array('target' => APPLICATION_PATH . '/uploads/imagethumbnail.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageResize', array('value' => ''));

$form->getElement('file')->addFilter('ImageResize', array('target' => APPLICATION_PATH . '/uploads/imageresize.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageCrop', array('value' => ''));

$form->getElement('file')->addFilter('ImageCrop', array('target' => APPLICATION_PATH . '/uploads/imagecrop.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageFlip', array('value' => ''));

$form->getElement('file')->addFilter('ImageFlip', array('target' => APPLICATION_PATH . '/uploads/imageflip.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageRotate', array('value' => ''));

$form->getElement('file')->addFilter('ImageRotate', array('target' => APPLICATION_PATH . '/uploads/imagerotate.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageFilter', array('value' => ''));

$form->getElement('file')->addFilter('ImageFilter', array('target' => APPLICATION_PATH . '/uploads/imagefilter.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBackgroundColor', array('value' => ''));

$form->getElement('file')->addFilter('ImageBackgroundColor', array('target' => APPLICATION_PATH . '/uploads/imagebackgroundcolor.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderColor', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderColor', array('target' => APPLICATION_PATH . '/uploads/imagebordercolor.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderWidth', array('value' => ''));

$form->getElement('file')->addFilter('ImageBorderWidth', array('target' => APPLICATION_PATH . '/uploads/imageborderwidth.txt', 'overwrite' => true));

$form->addElement('hidden', 'imageBorderHeight', array('

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

6分46秒

Java零基础-001-文件扩展名的显示

2分5秒

尚硅谷_Python基础_16_文件的扩展名.avi

19分49秒

09_手机外部文件存储_保存文件.avi

10分36秒

06_手机内部文件存储_保存文件.avi

8分28秒

批量重命名文件和文件夹,1行代码搞定,Python自动化办公很简单

1时0分

072_EGov教程_保存核准件并文件上传

6分4秒

如何按时间周期保存或备份已处理的文件?

4分35秒

104 - 尚硅谷 - SparkCore - 核心编程 - RDD - 文件读取与保存

11分16秒

11_手机外部文件存储2_保存读取.avi

7分27秒

第十八章:Class文件结构/10-字节码数据保存到excel中的操作

1分45秒

重新设计的文件管理器

6分15秒

入门案例!批量识别发票自动保存为Excel文件,1行Python代码实现(支持PDF格式)

领券