手工在SAE上配置WordPress上传图片功能

今天刚刚在SAE上把WordPress从v3.5.1升级到v3.5.2,升级过程都很顺利。
升级过程我也先小结一下,为了日后再次升级时做准备。在升级前很有必要仔细看一下官方的升级方法 🙂

(1)首先把WordPress插件全部停掉;

(2)备份数据库;进入SAE应用控制台 > MySQL > 管理MySQL

(3)备份svn中WordPress-v3.5.1全部源代码;

(4)从官方下载当前最新版本v3.5.2.zip源码;

(5)直接删除SVN中“wp-admin”和“wp-includes”文件夹下源码,官方不建议直接删除“wp-content”文件夹因为里面有自定义主题,如果自定义主题不是修改WordPress自带主题,那么就可以放心直接删除;

(6)把下载当前最新的v3.5.2源码直接全部覆盖到svn中对应目录中。

(7)在svn中提交对所有代码的修改(因为v3.5.2与v3.5.1的wp-config.php配置文件完全相同,所有直接用之前的配置文件就好);

(8)登录你的WordPress后台(/wp-admin),会提示升级数据库,点“升级”就好,升级过程很短也很顺利。

(9)好了。如果一切都很顺利,手工升级WordPress就算大功告成了。

现在说就本文重点,在SAE上配置WordPress上传图片功能:

(1)在SAE应用控制台,Strong上创建一个新domain,这个domain就要填在下面的配置文件中;

(2)在应该根目录下新建”sae.php”文件;

<?php
/**设置SAE应用里的Storage的Domain名(域名)**/
/**Set the domain name of storage belongs to your app.  **/

define('SAE_STORAGE',wordpress); //请把这里的wordpress修改为你的Storage域名
                                 //Please change "wordpress" to your own domain.

/**这里是设置文件上传的路径和文件路径的URL,不要更改**/
/**Here are the dir path & url path of the uploaded files,leave this, DO NOT Change.**/
define('SAE_DIR','saestor://'.SAE_STORAGE.'/uploads');
define('SAE_URL', 'http://' . $_SERVER['HTTP_APPNAME'] . '-'.SAE_STORAGE.'.stor.sinaapp.com/uploads');

(3)打开“应用/wp-admin/includes/file.php”;

注释掉下面 for SAE begin 和 for SAE end 中的代码:

	if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) {
		if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )
			$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
		else
			$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];

		return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $error_path ) );
	}

// for SAE begin
	// Set correct file permissions
	//$stat = stat( dirname( $new_file ));
	//$perms = $stat['mode'] & 0000666;
	//@ chmod( $new_file, $perms );
	// for SAE end

(4)打开“应用/wp-includes/functions.php”

在“require( ABSPATH . WPINC . ‘/option.php’ );”上面增加代码:

//for SAE begin
include( ABSPATH . '/sae.php' );  //调用SAE的Storage文件域名设置  //for SAE
//for SAE end

注释部分代码并增加部分代码,见下面代码:

function wp_mkdir_p( $target ) {
/*
	$wrapper = null;

	// strip the protocol
	if( wp_is_stream( $target ) ) {
		list( $wrapper, $target ) = explode( '://', $target, 2 );
	}

	// from php.net/mkdir user contributed notes
	$target = str_replace( '//', '/', $target );

	// put the wrapper back on the target
	if( $wrapper !== null ) {
		$target = $wrapper . '://' . $target;
	}
*/
	//for SAE begin
	//from php.net/mkdir user contributed notes
	if ( substr($target, 0, 10) == 'saestor://' ) {
		return true;
	}
	$target = str_replace( '//', '/', $target );
	//for SAE end

	// 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;
}

在“$basedir = $dir;”上面增加两行代码:

// for SAE begin
$dir = SAE_DIR;
$url = SAE_URL;
//for SAE end

$basedir = $dir;

在注释说明上增加代码见下面代码:

// for SAE begin
if ( !function_exists('utf8_encode') ) {
	function utf8_encode($str) {
		$encoding_in = mb_detect_encoding($str);
		return mb_convert_encoding($str, 'UTF-8', $encoding_in);
	}
}
// for SAE end

/**
 * Send a HTTP header to limit rendering of pages to same origin iframes.
 *
 * @link https://developer.mozilla.org/en/the_x-frame-options_response_header
 *
 * @since 3.1.3
 * @return none
 */

好了。配置完成了。测试一下上传图片吧。

wordpress-logo-stacked-rgb

大功告成 🙂

评论已关闭。