ThinkPHP file Upload

Source: Internet
Author: User
FileAction. class. php & lt ;? PhpclassFileActionextendsAction {functionindex () {$ fileM (& amp; #39; file & amp; #39;); $ list $ file-& gt; select (); $ this-& gt; assign (...

FileAction. class. php

  1. Class FileAction extends Action {
  2. Function index (){
  3. $ File = M ('file ');
  4. $ List = $ file-> select ();
  5. $ This-> assign ('filelist', $ list );
  6. $ This-> display ();
  7. }
  8. Function upload (){
  9. // Submit the file upload address to him. after the upload is complete, a message is returned for the file to be written to the database.
  10. If (emptyempty ($ _ FILES )){
  11. $ This-> error ('upload file is required ');
  12. } Else {
  13. $ A = $ this-> up ();
  14. If (isset ($ )){
  15. // Custom c method for writing data to the database
  16. If ($ this-> c ($ )){
  17. $ This-> success ('upload successful ');
  18. }
  19. Else {
  20. $ This-> error ('database writing failed ');
  21. }
  22. } Else {
  23. $ This-error ('file Upload exception, please contact the system Postmaster ');
  24. }
  25. }
  26. }
  27. Private function c ($ data ){
  28. $ File = M ('file ');
  29. $ Num = '0 ';
  30. For ($ I = 0; $ I <count ($ data)-1; $ I ++ ){
  31. $ Data ['filename'] = $ data [$ I] ['savename'];
  32. If ($ file-> data ($ data)-> add ())
  33. {
  34. $ Num ++;
  35. }
  36. }
  37. If ($ num = count ($ data)-1)
  38. {
  39. Return true;
  40. } Else
  41. {
  42. Return false;
  43. }
  44. }
  45. Private function up (){
  46. // Call the thinkphp-related file Upload class
  47. Import ('@. Org. uploadfile'); // Copy the upload class UploadFile. class. php to the Lib/Org folder.
  48. $ Upload = new UploadFile ();
  49. $ Upload-> maxSize = '000000'; // The default value is-1, which does not limit the upload size.
  50. $ Upload-> savePath = './Public/Upload/'; // The storage path is recommended to be saved in the same directory as the master file or in the same directory.
  51. $ Upload-> saveRule = uniqid; // rules for saving file names uploaded
  52. $ Upload-> uploadReplace = true; // whether to overwrite a file with the same name
  53. $ Upload-> allowExts = array ('jpg ', 'jpeg', 'PNG ', 'GIF'); // specifies the type of the file to be uploaded.
  54. $ Upload-> allowTypes = array ('image/png ', 'image/jpg', 'image/jpeg ', 'image/GIF'); // detect mime types
  55. $ Upload-> thumb = true; // whether to enable image file thumbnails
  56. $ Upload-> thumbMaxWidth = '000000 ';
  57. $ Upload-> thumbMaxHeight = '000000 ';
  58. $ Upload-> thumbPrefix ='s _, m _ '; // prefix of the thumbnail file
  59. $ Upload-> thumbRemoveOrigin = 1; // whether to delete the source image if a thumbnail is generated
  60. If ($ upload-> upload ()){
  61. $ Info = $ upload-> getUploadFileInfo ();
  62. Return $ info;
  63. } Else {
  64. $ This-> error ($ upload-> getErrorMsg (); //
  65. }
  66. }
  67. }
  68. ?>

Template file index.html

  1.  
  2.  
  3.  
  4.  
  5. Thumbnail:  
  6. Big chart:  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  

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.