国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > CMS > Wordpress > 正文

wordpress 文件上傳設(shè)置文件類型和大?。ń巧珯?quán)限)

2024-09-07 00:50:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

我們最初是簡(jiǎn)單的給所有用戶有上傳文件并且設(shè)置文件上傳可以使用media_handle_upload和wp_handle_upload 類似的函數(shù),其實(shí)都是調(diào)用wp_handle_upload函數(shù)

這個(gè)函數(shù)的第4個(gè)參數(shù),支持?jǐn)?shù)組,代碼如下:

  1. $access = array('test_form' => false, 'mimes' => array('jpeg' => 'image/jpeg'
  2.  'png' => 'image/png','gif' => 'image/gif','jpg' => 'image/jpeg')); 

這樣寫(xiě)就可以限制文件的類型,但是貌似對(duì)偽造的文件不好使,經(jīng)過(guò)測(cè)試可以使用一個(gè)過(guò)濾器,完美解決這個(gè)問(wèn)題,過(guò)濾器名稱:wp_handle_upload_prefilter,代碼如下:

  1. add_filter('wp_handle_upload_prefilter''custom_upload_filter' ); 
  2. function custom_upload_filter($file
  3.     $fileSize = $file['size'];//獲取文件大小 做自定義處理 
  4.     $info =getimagesize($file['tmp_name']);//獲取文件屬性 如果不是圖片將返回false 
  5.     return $file

讓用戶擁有上傳文件的權(quán)限

默認(rèn)情況下,有些用戶是不允許上傳文件的,你可以在主題的 functions.php 添加下面的代碼:

  1. //允許用戶投稿時(shí)上傳文件 
  2. if ( current_user_can('contributor') && !current_user_can('upload_files') ) 
  3.    add_action('admin_init''allow_contributor_uploads'); 
  4.  
  5.    function allow_contributor_uploads() { 
  6.       $contributor = get_role('contributor'); 
  7.       $contributor->add_cap('upload_files'); 

上面的代碼就是給 'contributor' 這個(gè)用戶角色添加了 'upload_files'(上傳文件)的權(quán)限。

限制用戶上傳文件的類型

首先,大家可以先了解一下 WordPress 默認(rèn)允許上傳的文件類型,打開(kāi)WordPress的 /wp-includes/functions.php 文件,然后搜索 function wp_get_mime_types 定位到那里,你就會(huì)看到詳細(xì)的文件類型:

  1. function wp_get_mime_types() { 
  2.  // Accepted MIME types are set here as PCRE unless provided. 
  3.  return apply_filters( 'mime_types'array
  4.  // Image formats 
  5.  'jpg|jpeg|jpe' => 'image/jpeg'
  6.  'gif' => 'image/gif'
  7.  'png' => 'image/png'
  8.  'bmp' => 'image/bmp'
  9.  'tif|tiff' => 'image/tiff'
  10.  'ico' => 'image/x-icon'
  11.  // Video formats 
  12.  'asf|asx|wax|wmv|wmx' => 'video/asf'
  13.  'avi' => 'video/avi'
  14.  'divx' => 'video/divx'
  15.  'flv' => 'video/x-flv'
  16.  'mov|qt' => 'video/quicktime'
  17.  'mpeg|mpg|mpe' => 'video/mpeg'
  18.  'mp4|m4v' => 'video/mp4'
  19.  'ogv' => 'video/ogg'
  20.  'mkv' => 'video/x-matroska'
  21.  // Text formats 
  22.  'txt|asc|c|cc|h' => 'text/plain'
  23.  'csv' => 'text/csv'
  24.  'tsv' => 'text/tab-separated-values'
  25.  'ics' => 'text/calendar'
  26.  'rtx' => 'text/richtext'
  27.  'css' => 'text/css'
  28.  'htm|html' => 'text/html'
  29.  // Audio formats 
  30.  'mp3|m4a|m4b' => 'audio/mpeg'
  31.  'ra|ram' => 'audio/x-realaudio'
  32.  'wav' => 'audio/wav'
  33.  'ogg|oga' => 'audio/ogg'
  34.  'mid|midi' => 'audio/midi'
  35.  'wma' => 'audio/wma'
  36.  'mka' => 'audio/x-matroska'
  37.  // Misc application formats 
  38.  'rtf' => 'application/rtf'
  39.  'js' => 'application/javascript'
  40.  'pdf' => 'application/pdf'
  41.  'swf' => 'application/x-shockwave-flash'
  42.  'class' => 'application/java'
  43.  'tar' => 'application/x-tar'
  44.  'zip' => 'application/zip'
  45.  'gz|gzip' => 'application/x-gzip'
  46.  'rar' => 'application/rar'
  47.  '7z' => 'application/x-7z-compressed'
  48.  'exe' => 'application/x-msdownload'
  49.  // MS Office formats 
  50.  'doc' => 'application/msword'
  51.  'pot|pps|ppt' => 'application/vnd.ms-powerpoint'
  52.  'wri' => 'application/vnd.ms-write'
  53.  'xla|xls|xlt|xlw' => 'application/vnd.ms-excel'
  54.  'mdb' => 'application/vnd.ms-access'
  55.  'mpp' => 'application/vnd.ms-project'
  56.  'docx' => 'application/vnd.openxmlformats- 
  57. officedocument.wordprocessingml.document', 
  58.  'docm' => 'application/vnd.ms-word.document.macroEnabled.12'
  59.  'dotx' => 'application/vnd.openxmlformats- 
  60. officedocument.wordprocessingml.template', 
  61.  'dotm' => 'application/vnd.ms-word.template.macroEnabled.12'
  62.  'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  63.  'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12'
  64.  'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
  65.  'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'
  66.  'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12'
  67.  'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12'
  68.  'pptx' => 'application/vnd.openxmlformats- 
  69. officedocument.presentationml.presentation', 
  70.  'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'
  71.  'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow'
  72.  'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'
  73.  'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template'
  74.  'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12'
  75.  'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12'
  76.  'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide'
  77.  'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12'
  78.  'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote'
  79.  // OpenOffice formats 
  80.  'odt' => 'application/vnd.oasis.opendocument.text'
  81.  'odp' => 'application/vnd.oasis.opendocument.presentation'
  82.  'ods' => 'application/vnd.oasis.opendocument.spreadsheet'
  83.  'odg' => 'application/vnd.oasis.opendocument.graphics'
  84.  'odc' => 'application/vnd.oasis.opendocument.chart'
  85.  'odb' => 'application/vnd.oasis.opendocument.database'
  86.  'odf' => 'application/vnd.oasis.opendocument.formula'
  87.  // WordPerfect formats 
  88.  'wp|wpd' => 'application/wordperfect'
  89.  ) ); 

=> 的前面為格式,后面為格式描述,如果你要禁止上傳其中的某些類型,可以參考下面的例子,將下面的代碼添加到主題的 functions.php 文件:

  1. //禁止上傳avi和mp4格式的文件 
  2. add_filter('upload_mimes''custom_upload_mimes'); 
  3. function custom_upload_mimes ( $existing_mimes=array() ) { 
  4. unset ($existing_mimes['avi']); 
  5. unset ($existing_mimes['mp4']); 
  6. return $existing_mimes
  7. }  

如果你還要禁止更多,可以按照 unset ($existing_mimes['格式']); 樣例添加即可,如果你僅僅只需要允許用戶上傳幾種類型而已,還可以通過(guò)下面的更簡(jiǎn)潔的方法,代碼添加到主題的functions.php 文件:

  1. //只允許上傳圖片文件 
  2. add_filter('upload_mimes''custom_upload_mimes'); 
  3. function custom_upload_mimes ( $existing_mimes=array() ) { 
  4. unset ($existing_mimes);//禁止上傳任何文件 
  5. $existing_mimes['jpg|jpeg|gif|png']='image/image';//允許用戶上傳jpg,gif,png文件 
  6. return $existing_mimes

如果你還要允許上傳其他格式,重復(fù)使用 $existing_mimes['格式']='描述';即可.

限制用戶上傳的文件大小

同樣在主題的 functions.php 文件中,添加下面的代碼:

  1. //限制上傳文件的最大體積 
  2. function max_up_size() { 
  3. return 500*1024; // 500 kb 
  4. add_filter('upload_size_limit''max_up_size');  

上面的例子是限制所有用戶上傳的文件的最大體積為 500 kb (1M =1024*1024)。

注意:主機(jī)空間和WordPress本身一般設(shè)置了允許上傳的文件的最大體積,所以在這里設(shè)置需要考慮到這點(diǎn).

限制不同用戶角色可上傳的文件類型及大小

其實(shí)上面已經(jīng)給出了限制類型和大小的方法,要根據(jù)不同用戶角色來(lái)限制,只需要添加角色判斷代碼即可,倡萌舉個(gè)綜合的例子:

不同用戶上傳的類型,代碼如下:

  1. function custom_upload_mimes ( $existing_mimes=array() ) { 
  2.  unset ($existing_mimes);//禁止上傳任何文件 
  3.  if( current_user_can( 'publish_posts' ) && !current_user_can( 'publish_pages' ) ) { 
  4.   //允許作者(Author)上傳的類型 
  5.   $existing_mimes['jpg|jpeg|gif|png']='image/image';//允許用戶上傳jpg,gif,png 
  6. 文件 
  7.   $existing_mimes['zip']='application/zip'//允許用戶上傳zip壓縮包 
  8.   $existing_mimes['pdf']='application/pdf'//允許用戶上傳pdf文件 
  9.  }elseif( current_user_can( 'edit_posts' ) && !current_user_can( 'publish_posts' ) ) 
  10.   //允許投稿者(Contributor)上傳的類型 
  11.   $existing_mimes['jpg|jpeg|gif|png']='image/image'
  12.   $existing_mimes['pdf']='application/pdf'
  13.  }else
  14.   //其他用戶角色上傳的類型 
  15.   $existing_mimes['jpg|jpeg|gif|png']='image/image'
  16.  } 
  17.  return $existing_mimes
  18. //不同用戶上傳的大小 
  19. function max_up_size() { 
  20.  if( current_user_can( 'publish_posts' ) && !current_user_can( 'publish_pages' ) ) { 
  21.   return 2048*1024; // 允許作者(Author)上傳 2M 
  22.  }elseif( current_user_can( 'edit_posts' ) && !current_user_can( 'publish_posts' ) ) 
  23.   return 1024*1024; // 允許投稿者(Contributor)上傳 1M 
  24.  }else
  25.   return 500*1024; // 其他用戶角色上傳 500 kb 
  26.  } 
  27. //只對(duì)非管理員執(zhí)行這兩個(gè)函數(shù)(即:對(duì)管理員不生效) 
  28. if( !current_user_can( 'manage_options' ) ) { 
  29.  add_filter('upload_mimes''custom_upload_mimes'); 
  30.  add_filter('upload_size_limit''max_up_size'); 

大家只要靈活使用 if 語(yǔ)句判斷不同的角色賦予不同的權(quán)限即可.

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄冈市| 防城港市| 乌恰县| 花莲县| 隆昌县| 思茅市| 扎鲁特旗| 县级市| 靖江市| 霍城县| 通化市| 开阳县| 日喀则市| 孝义市| 太湖县| 余江县| 洛宁县| 无锡市| 时尚| 南岸区| 德惠市| 门头沟区| 都昌县| 咸阳市| 平顺县| 深水埗区| 南城县| 池州市| 固镇县| 石门县| 遂溪县| 康马县| 红安县| 正定县| 绥棱县| 南投县| 枞阳县| 新乡市| 许昌市| 麻栗坡县| 荆州市|