实际上,有一种更简单的方法可以做到这一点.您需要做的就是将false作为Zend_File_Transfer_Adapter_Http对象的getFileName方法的第二个参数传递.然后,您可以通过向其附加userID来重命名该文件,或者解析文件名以获得扩展名(如果您也喜欢).
// upload a file called myimage.jpg from the formfield named "image". $uploaded_file = new Zend_File_Transfer_Adapter_Http(); $uploaded_file->setDestination('/your/path/'); try { // upload the file $uploaded_file->receive(); } catch (Zend_File_Transfer_Exception $e) { $e->getMessage(); } $file_name = $uploaded_file->getFileName('image', false); // this outputs "myimage.jpg" $file_path = $uploaded_file->getFileName('image'); // this outputs "/your/path/myimage.jpg" // now use the above information to rename the file
我设法通过设置过滤器来做到这一点.请注意,我没有设置目标路径.
$adapter= new Zend_File_Transfer_Adapter_Http(); $adapter->addFilter('Rename',array('target' => WWW_ROOT . '/photos/' . $this->memberId . '.jpg')); $adapter->receive();