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

首頁 > CMS > Wordpress > 正文

wordpress文件上傳到服務(wù)器改變文件名

2024-09-07 00:50:28
字體:
供稿:網(wǎng)友

沒有人能保證自己上傳的文件時不會存在相同名字的,那么要如何解決此問題呢,wordpress博客中我們可以利用wp_handle_upload_prefilter來解決此問題,下面演示一個實例.

例,利用圖片高與寬生+文件名成名字,代碼如下:

  1. add_filter( 'wp_handle_upload_prefilter''modify_uploaded_file_names', 20); 
  2. function modify_uploaded_file_names( $image ) { 
  3.     // Get default name of uploaded file and set to variable 
  4.     $imagename = $image['name']; 
  5.     // Case switch for multiple file extensions 
  6.     switch ( $image['type'] ) { 
  7.         case 'image/jpeg' : 
  8.             $suffix = 'jpg'
  9.             break
  10.         case 'image/png' : 
  11.             $suffix = 'png'
  12.             break
  13.         case 'image/gif' : 
  14.             $suffix = 'gif'
  15.             break
  16.     } 
  17.     // Get size of uploaded image and assign to variable 
  18.     $imagesize = getimagesize($image); 
  19.     // Re-structure uploaded image name 
  20.     $image['name'] = "{$imagesize[0]}x{$imagesize[1]}-{$imagename}.{$suffix}"
  21.  
  22.     return $image

例,利用年月日時分秒+千位毫秒整數(shù)

以wordpress 3.2.1為例,打開“wp-admin/includes/file.php” m.survivalescaperooms.com文件,找到第327行這段代碼:

  1. // Move the file to the uploads dir 
  2. $new_file = $uploads['path'] . "/$filename"
  3. if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) 
  4.         return $upload_error_handler$file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) ); 

將其修改為如下代碼:

  1. // Move the file to the uploads dir 
  2. $new_file = $uploads['path'] . "/".date("YmdHis").floor(microtime()*1000).".".$ext;  
  3. if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) 
  4.         return $upload_error_handler$file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) ); 

保存,重新上傳文件,這樣,新上傳的文件,就會自動保存為“年月日時分秒+千位毫秒整數(shù)”的新文件名,并保存到相應(yīng)的年月文件夾之下了.

提醒你,這兩種方法個人覺得后者更適合我們一些哦,因為按年月日時分秒+千位毫秒整數(shù)不會出現(xiàn)重復(fù)名字,而按圖片高與寬生+文件名成名字還有可能存在重復(fù)名字.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汝南县| 阿勒泰市| 大埔县| 漳平市| 六安市| 灵川县| 灌南县| 明水县| 二手房| 巴彦淖尔市| 太白县| 抚州市| 宁武县| 德钦县| 和田市| 开封市| 江华| 望谟县| 潢川县| 梨树县| 西宁市| 新蔡县| 九龙县| 铁岭市| 宜良县| 车致| 山东省| 自治县| 慈利县| 尼玛县| 江达县| 韶关市| 阳江市| 宜都市| 霍林郭勒市| 台安县| 双城市| 定南县| 蕉岭县| 无极县| 广德县|