Rename the file uploaded by ZendFramework

Source: Internet
Author: User
Rename the file uploaded by ZendFramework
<无详细内容>

  1. // Specifies the specific class of the instantiated file.
  2. $ FName = $ this-> _ request-> getPost ('fname ');
  3. $ Adapter = new Zend_File_Transfer_Adapter_Http ();
  4. // Folder for storing uploaded files
  5. $ Adapter-> setDestination ('/opt/lampp/htdocs/blog/upload ');
  6. // Upload configuration
  7. $ Adapter
  8. -> AddValidator ('extension', false, $ configs ['extension']) // file format restriction
  9. -> AddValidator ('size', false, array ('min' => floatval ($ configs ['minsize']),
  10. 'Max' => floatval ($ configs ['maxsize']) // sets the size of the uploaded file to be 1-2 MB.
  11. -> AddValidator ('count', false, array ('min' => intval ($ configs ['mincount']),
  12. 'Max' => intval ($ configs ['maxcount']); // Number of uploaded files
  13. // Rename the configuration
  14. $ FileInfo = $ adapter-> getFileInfo (); // get basic configuration
  15. $ ExtName = $ this-> getExtension ($ fileInfo); // get the extension
  16. $ Filename = md5 (time () + $ fileInfo ['ffile '] ['name']).'. '. $ extName; // rename
  17. $ Adapter-> addFilter ('rename', array ('target' => $ filename, 'overwrite' => true); // Rename the filter.
  18. // Return the current information after the Upload
  19. If (! $ Adapter-> receive ())
  20. {
  21. $ Messages = $ adapter-> getMessages (); // detection
  22. // Zend_Debug: dump ($ messages );
  23. $ Message = '';
  24. If (is_array ($ messages ))
  25. {
  26. Foreach ($ messages as $ k => $ v)
  27. {
  28. $ Message. = $ k. ':'. $ v .'
    ';
  29. }
  30. }
  31. Else
  32. {
  33. $ Message = $ messages;
  34. }
  35. }
  36. Else
  37. {
  38. $ This-> view-> message = 'upload successful! ';
  39. }

  1. /**
  2. * Get the file extension
  3. * @ Param String $ name File noun
  4. * @ Author
  5. */
  6. Public function getExtension ($ name)
  7. {
  8. $ Fname = '';
  9. If ($ name)
  10. {
  11. Foreach ($ name as $ val)
  12. {
  13. $ Fname = $ val ['name'];
  14. }
  15. $ Exts = @ split ("[/\.]", $ fname );
  16. $ N = count ($ exts)-1;
  17. $ Exts = $ exts [$ n];
  18. Return $ exts;
  19. }
  20. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.