程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> php同步技巧

php同步技巧

編輯:PHP基礎知識
 

如果兩台服務器 一個是主站服務器 一個是圖片服務器(流量太大,所以分開),
如果圖片服務器的圖片不能及時從主站同步過來的時候
訪問img.aaa.com/11/22/1.jpg 這樣的圖片 就404了。
這種情況下,可以用下面的方法解決:
在圖片服務器做404的nignx配置:

location / {
   error_page 404 = /404_get_img.php;
}

上面即是說 如果訪問該域名下,出現404的 就執行404_get_img.php
404_get_img.php可以這樣寫:


if ($_SERVER["SERVER_NAME"] == "img.aaa.com") {
$uri = $_SERVER["REQUEST_URI"];
if (!empty ($uri)) {
$temp_der_arr = explode("/", $uri);
$count_t = count($temp_der_arr) - 1;
$dir = "";
for ($i = 1; $i < $count_t; $i++) { //循環創建多層目錄
$dir .= $temp_der_arr[$i] . "/";
if (!file_exists($dir)) {
MkdirAll($dir, '777');
}
}
header("Content-type: image/gif");
$content = file_get_contents("http://www.aaa.com" . $uri);//從主站獲取圖片
//可以限制獲取的圖片必須大於某個數值時 才寫入文件,這個數值根據實際情況設定。
if(strlen($content)>11772) {
$uri1 = substr($uri, 1);
file_put_contents($uri1, $content);
echo $content;
die;
}
}
}

//用FTP創建一個目錄
function FtpMkdir($truepath, $mmode, $isMkdir = true) {
global $cfg_basedir, $cfg_ftp_root, $g_ftpLink;
OpenFtp();
$ftproot = ereg_replace($cfg_ftp_root . '$', '', $cfg_basedir);
$mdir = ereg_replace('^' . $ftproot, '', $truepath);
if ($isMkdir)
ftp_mkdir($g_ftpLink, $mdir);
return ftp_site($g_ftpLink, "chmod $mmode $mdir");
}
//用FTP改變一個目錄的權限
function FtpChmod($truepath, $mmode) {
return FtpMkdir($truepath, $mmode, false);
}
//打開FTP連接
function OpenFtp() {
global $cfg_basedir, $cfg_ftp_host, $cfg_ftp_port;
global $cfg_ftp_user, $cfg_ftp_pwd, $cfg_ftp_root, $g_ftpLink;
if (!$g_ftpLink) {
if ($cfg_ftp_host == "") {
echo "由於你的站點的PHP配置存在限制,程序嘗試用FTP進行目錄操作,你必須在後台指定FTP相關的變量!";
exit ();
}
$g_ftpLink = ftp_connect($cfg_ftp_host, $cfg_ftp_port);
if (!$g_ftpLink) {
echo "連接FTP失敗!";
exit ();
}
if (!ftp_login($g_ftpLink, $cfg_ftp_user, $cfg_ftp_pwd)) {
echo "登陸FTP失敗!";
exit ();
}
}
}
//通用的創建目錄的函數
function MkdirAll($truepath, $mmode) {
global $cfg_ftp_mkdir, $isSafeMode;
if ($isSafeMode || $cfg_ftp_mkdir == '是') {
return FtpMkdir($truepath, $mmode);
} else {
if (!file_exists($truepath)) {
mkdir($truepath, 0777);
chmod($truepath, 0777);
return true;
} else {
return true;
}
}
}

 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved