PHP file Operations
Copy the database file path to another directory
Code:
<?php date_default_timezone_set (' Asia/shanghai ');
Set time zone $db _host = ' 127.0.0.1 ';
$db _name = ' Test_qyg_datas ';
$db _user = ' root ';
$db _pwd = ' xxx ';
$mysqli = new Mysqli ($db _host, $db _user, $db _pwd, $db _name);
$mysqli->set_charset ("UTF8"); $sql _array = Array (' Select image from Buser_goods_list where Image <> ' ORDER by bid ', ' Select image from Suppl Ier_buser_goods_list where Image <> "ORDER by bid ', ' SELECT image from Works_sku where Image <> '
Y id ', ' SELECT image from Supplier_works_sku where Image <> ' ORDER by ID '; $fs = Directory_separator;
File Separator $root = "{$fs}data{$fs}www{$fs}test";
$root _CP = "{$fs}data{$fs}www{$fs}IMG_CP";
$log = ' Log.log '; foreach ($sql _array as $key => $value) {file_put_contents ($log, date (' y-m-d h:i:s ').
"Query sql: $value \ r \ n", file_append);
$result = $mysqli->query ($value); if ($result = = False) {file_put_contents ($log, date (' y-m-d h:i:s ').
"SQLERROR Query SQL failed: $value \ r \ n", file_append);Continue
try{while ($row = $result->fetch_assoc ()) {if Empty ($row [' image ']) continue;
$images = Explode (', ', $row [' image ']); foreach ($images as $k => $img) {if (Stripos ($img, ' http ')!== false) {//File path contains http $preg = '/^ (http:\/\ /? [^\/]+)/I ';
Matching HTTP domain name regular preg_match ($preg, $img, $res);
$img = Str_replace ($res [0], $img); } if (!file_exists ($root. $img)) {file_put_contents ($log, date (' y-m-d h:i:s ').
"ERROR file does not exist: $img \ r \ n", file_append);
Continue
$img _index = Strripos ($img, '/') + 1;
$folder = substr ($img, 0, $img _index);
$file = substr ($img, $img _index); $root _cp_tmp = $root _cp.
$folder; Determine if the folder exists if (!file_exists ($root _cp_tmp)) {$res = mkdir (Iconv ("UTF-8", "GBK", $root _cp_tmp), 0777, true);//Create The folder if ($res = = True) {file_put_contents ($log, date (' y-m-d h:i:s ').
"TRUE to create a directory: $root _cp_tmp\r\n", file_append); }else{file_put_contents ($log, date (' YM-d h:i:s ').
"ERROR Create directory failed: $root _cp_tmp\r\n", file_append);
Continue
}//Execute Copy File command exec ("CP $root $img $root _cp_tmp", $result _info); if (Empty ($result _info)) {file_put_contents ($log, date (' y-m-d h:i:s ').
"TRUE replication success: $img \ r \ n", file_append); }else{file_put_contents ($log, date (' y-m-d h:i:s '). "ERROR Replication failed:". Json_encode ($result _info).
"File: $img \ r \ n", file_append); }}}catch (Exception $e) {file_put_contents ($log, date (' y-m-d h:i:s '). "Syserror an error:". $e->getmessage ().
"\ r \ n", file_append); }
}