在新浪云中建立wordpress网站,使用默认的wordpress版本比较低,还无法更新,使用代码覆盖的方法更新wordpress,却发现无法上传图片,原来是新浪云使用的SAE自带的存储器,而不是wordpress默认的。因此需要修改wordpress的源代码,以便可以上传文件。
替换wp-includes/functions.php中的两个函数 wp_mkdir_p和wp_upload_dir
function wp_mkdir_p( $target ) {
// from php.net/mkdir user contributed notes
if ( substr($target, 0, 10) == 'saestor://' ) {
return true;
}
$target = str_replace( '//', '/', $target );
// safe mode fails with a trailing slash under certain PHP versions.
$target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
if ( empty($target) )
$target = '/';
if ( file_exists( $target ) )
return @is_dir( $target );
// Attempting to create the directory may clutter up our display.
if ( @mkdir( $target ) ) {
$stat = @stat( dirname( $target ) );
$dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
@chmod( $target, $dir_perms );
return true;
} elseif ( is_dir( dirname( $target ) ) ) {
return false;
}
// If the above failed, attempt to create the parent node, then try again.
if ( ( $target != '/' ) && ( wp_mkdir_p( dirname( $target ) ) ) )
return wp_mkdir_p( $target );
return false;
}
function wp_upload_dir( $time = null ) {
global $switched;
$siteurl = get_option( 'siteurl' );
$upload_path = get_option( 'upload_path' );
$upload_path = trim($upload_path);
$main_override = is_multisite() && defined( 'MULTISITE' ) && is_main_site();
if ( empty($upload_path) ) {
$dir = WP_CONTENT_DIR . '/uploads';
} else {
$dir = $upload_path;
if ( 'wp-content/uploads' == $upload_path ) {
$dir = WP_CONTENT_DIR . '/uploads';
} elseif ( 0 !== strpos($dir, ABSPATH) ) {
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH
$dir = path_join( ABSPATH, $dir );
}
}
if ( !$url = get_option( 'upload_url_path' ) ) {
if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
$url = WP_CONTENT_URL . '/uploads';
else
$url = trailingslashit( $siteurl ) . $upload_path;
}
if ( defined('UPLOADS') && !$main_override && ( !isset( $switched ) || $switched === false ) ) {
$dir = ABSPATH . UPLOADS;
$url = trailingslashit( $siteurl ) . UPLOADS;
}
if ( is_multisite() && !$main_override && ( !isset( $switched ) || $switched === false ) ) {
if ( defined( 'BLOGUPLOADDIR' ) )
$dir = untrailingslashit(BLOGUPLOADDIR);
$url = str_replace( UPLOADS, 'files', $url );
}
// for SAE
$dir = 'saestor://wordpress/uploads';
$url = 'http://' . $_SERVER['HTTP_APPNAME'] . '-wordpress.stor.sinaapp.com/uploads';
$bdir = $dir;
$burl = $url;
$subdir = '';
if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
// Generate the yearly and monthly dirs
if ( !$time )
$time = current_time( 'mysql' );
$y = substr( $time, 0, 4 );
$m = substr( $time, 5, 2 );
$subdir = "/$y/$m";
}
$dir .= $subdir;
$url .= $subdir;
$uploads = apply_filters( 'upload_dir', array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'basedir' => $bdir, 'baseurl' => $burl, 'error' => false ) );
// Make sure we have an uploads dir
if ( ! wp_mkdir_p( $uploads['path'] ) ) {
$message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $uploads['path'] );
return array( 'error' => $message );
}
return $uploads;
}
修改
wp-admin/includes/file.php
/*** 注释如下代码 ***/
// Set correct file permissions.
$stat = stat ( dirname ( $new_file ) );
$perms = $stat ['mode'] & 0000666;
@ chmod ( $new_file, $perms );
修改后的wp-admin/includes/file.php代码片段
// Set correct file permissions.
//$stat = stat( dirname( $new_file ));
//$perms = $stat['mode'] & 0000666;
//@ chmod( $new_file, $perms );