您的位置: 首页 / PHP / 强化 WordPress 2.0.1 文件上传功能

强化 WordPress 2.0.1 文件上传功能

Published at Feb 10, 11am / Keywords: , ,

WordPress 2.0.1 upload functionWordPress 2.0.1 相对于 2.0 的一个重要修正就是“可以指定文件上传路径”。但这一功能相对 WordPress 1.5.x 版本还不够完善。在 WP 1.5 中,我们可以设定上传文件存放的绝对路径,同时可以设定访问上传文件的 URL 前缀。通过这个功能,我们可以将文件上传到其它路径下,从而借助服务器设置轻松实现一些功能(例如防盗链);而对于访问量巨大的网站,像图片、附件这样的静态内容可以通过单独使用一个静态编译的 apache,减小服务器负担,提高吞吐量。

在 WP 2.0.1 中,这个功能被弱化了。只能设定文件存放的相对路径,而且无法设定 URL 前缀。对于从 1.5.x 版一直使用这个功能的网站(例如本站)来说,移植是十分麻烦的。于是我通过简单修改 WordPress,为 WordPress 2.0.1 加上了这个功能。(懒得自己改的朋友可以在这里下载改好的文件

在 wp-includes/functions-post.php 文件中,搜索 function wp_upload_dir(),按照如下修改这个函数:

  1. <?php
  2. // Returns an array containing the current upload directory's path and url, or an error message.
  3. function wp_upload_dir() {
  4.     $siteurl = get_settings('siteurl');
  5.     //prepend ABSPATH to $dir and $siteurl to $url if they're not already there
  6.     $path = str_replace(ABSPATH, '', trim(get_settings('upload_path')));
  7.     if (substr($path,0,1) == '/') {
  8.         // an absolute path
  9.         $dir = $path;
  10.         $url = trailingslashit(get_settings('fileupload_url'));
  11.     } else {
  12.         $dir = ABSPATH . $path;
  13.         $url = trailingslashit($siteurl) . $path;
  14.     }
  15.  
  16.     if ( $dir == ABSPATH ) { //the option was empty
  17.         $dir = ABSPATH . 'wp-content/uploads';
  18.     }
  19.  
  20.     if ( defined('UPLOADS') ) {
  21.         $dir = ABSPATH . UPLOADS;
  22.         $url = trailingslashit($siteurl) . UPLOADS;
  23.     }
  24.  
  25.     if ( get_settings('uploads_use_yearmonth_folders')) {
  26.         // Generate the yearly and monthly dirs
  27.         $time = current_time( 'mysql' );
  28.         $y = substr( $time, 0, 4 );
  29.         $m = substr( $time, 5, 2 );
  30.         $dir = $dir . "/$y/$m";
  31.         $url = $url . "$y/$m";
  32.     }
  33.  
  34.     // Make sure we have an uploads dir
  35.     if ( ! wp_mkdir_p( $dir ) ) {
  36.         $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $dir);
  37.         return array('error' => $message);
  38.     }
  39.  
  40.     $uploads = array('path' => $dir, 'url' => $url, 'error' => false);
  41.     return apply_filters('upload_dir', $uploads);
  42. }
  43. ?>

再打开 wp-admin/options-misc.php,改成如下内容:

  1. <?php
  2. require_once('admin.php');
  3.  
  4. $title = __('Miscellaneous Options');
  5. $parent_file = 'options-general.php';
  6.  
  7. include('admin-header.php');
  8.  
  9. ?>
  10.  
  11. <div class="wrap"> 
  12. <h2><?php _e('Miscellaneous Options') ?></h2> 
  13. <form method="post" action="options.php">
  14.  
  15. <fieldset class="options">
  16. <legend><?php _e('Uploading'); ?></legend>
  17. <table class="editform optiontable">
  18. <tr valign="top">
  19. <th scope="row"><?php _e('Store uploads in this folder'); ?>:</th>
  20. <td><input name="upload_path" type="text" id="upload_path" class="code" value="<?php echo get_settings('upload_path'); ?>" size="40" />
  21. <br />
  22. <?php _e('Default is <code>wp-content/uploads</code>'); ?>
  23. </td>
  24. </tr>
  25. <tr valign="top">
  26. <th scope="row"><?php _e('Uploads URL'); ?>:</th>
  27. <td><input name="fileupload_url" type="text" id="fileupload_url" class="code" value="<?php echo get_settings('fileupload_url'); ?>" size="40" />
  28. </td>
  29. </tr>
  30. <tr>
  31. <td></td>
  32. <td>
  33. <label for="uploads_use_yearmonth_folders">
  34. <input name="uploads_use_yearmonth_folders" type="checkbox" id="uploads_use_yearmonth_folders" value="1" <?php checked('1', get_settings('uploads_use_yearmonth_folders')); ?> />
  35. <?php _e('Organize my uploads into month- and year-based folders'); ?>
  36. </label>
  37. </td>
  38. </tr>
  39. </table>
  40. </fieldset>
  41.  
  42. <p><input name="use_linksupdate" type="checkbox" id="use_linksupdate" value="1" <?php checked('1', get_settings('use_linksupdate')); ?> />
  43. <label for="use_linksupdate"><?php _e('Track Links&#8217; Update Times') ?></label></p>
  44. <p>
  45. <label><input type="checkbox" name="hack_file" value="1" <?php checked('1', get_settings('hack_file')); ?> /> <?php _e('Use legacy <code>my-hacks.php</code> file support') ?></label>
  46. </p>
  47.  
  48. <p class="submit">
  49. <input type="hidden" name="action" value="update" />
  50. <input type="hidden" name="page_options" value="hack_file,use_linksupdate,uploads_use_yearmonth_folders,upload_path,fileupload_url" /> 
  51. <input type="submit" name="Submit" value="<?php _e('Update Options') ?> &raquo;" />
  52. </p>
  53. </form> 
  54. </div>
  55.  
  56. <?php include('./admin-footer.php'); ?>

进入管理后台,选项->杂项 即可进行设置。以上代码在本站测试正常并已经应用。

收藏和分享本文 17fav 收藏本文

发表您的观点或推荐本文 Loading...

11 Responses

  1. Feb 10, 7pm / LINK / REPLY
    Gravatar

    不错,我还在用1.5,不过还是用了插件来解决这个问题,请教一下,你post里面图片和文字的融合用什么插件作的?

  2. Feb 10, 7pm / LINK / REPLY
    Gravatar

    不知道是不是feedburner的rss有问题,bloglines上没更新提示,这两次倒都是先看到了Blogging Pro China的引用文章

  3. Feb 10, 11pm / LINK / REPLY
    Gravatar

    to netear:没有用任何插件,wp2.0的图片上传机制已经足够好用了。:mrgreen:

  4. Feb 11, 5pm / LINK / REPLY
    Gravatar

    啊哈,也是dreamhost的用户啊……
    我也才搬去不久,百废待兴哪…
    模板很漂亮:)
    你的图片格式是怎么修改css来实现的呢?

  5. Feb 11, 11pm / LINK / REPLY
    Gravatar

    贵站用的哪款代码高亮插件?

  6. Feb 12, 10pm / LINK / REPLY
    Gravatar

    to moyii: coolcode

  7. Feb 13, 3pm / LINK / REPLY
    Gravatar

    to iStef:

    如何将图片嵌入到文字中,而不是另起一行呢?

  8. Feb 13, 4pm / LINK / REPLY
    Gravatar

    to netear: <img src=”xxx” alt=”xxx” align=”left” />

    或者 <img src=”xxx” alt=”xxx” style=”float:left” />

    我比较偏爱后一种写法,兼容性更好

  9. May 16, 10am / LINK / REPLY
    Gravatar

    文件不能下载 ?

  10. Oct 30, 9am / LINK / REPLY
    Gravatar

    hehe不错,很强啊,,blog做得也很不错。不过文件不能下

Now, It's your Turn!

BACK TO Article / Comments