普通网友 2015-05-21 16:02
浏览 28

PHP文件没有拉入我的.txt文件

I have an admin store dashboard with an update button that triggers a php file which empties a mySQL database that then puts in new data from three .txt files (they are new stores). There is an issue where the table just gets wiped and/or the data from the .txt file is not being sent. I recently upgraded my PHP to 5.4 from 5.3 and am unsure what the issue is. Can anyone recommend me a suggestion on how to fix this issue?

enter image description here

PHP:

<?php
header('Content-Type: application/json; charset=UTF-8');
$argv = $_SERVER['argv'];
$totalArgv = count($argv);
// Use PDO to connect to the DB
$dsn = 'mysql:dbname=busaweb_stores;host=127.0.0.1';
$dsn_training = 'mysql:dbname=busaweb_busaedu;host=127.0.0.1';
$user = 'busaweb_*****';
$password = '*****';

try {
$dbs = new PDO($dsn, $user, $password);
$dbt = new PDO($dsn_training, $user, $password);
$dbs->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$dbt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo 'Connected to SQL Server';
} 
catch (PDOException $e) {
die_with_error('PDO Connection failed: ' . $e->getMessage());
}

//Check request
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
$action = $_POST["action"];
switch($action) { //Switch case for value of action
  case "move":
    move_files(); 
    echo_success("Files have been moved.");
    break;
  case "update-stores":
    $count = update_stores_db($dbs);
    echo_success("DB Updated.<br>" . $count . " Stores Geocoded.");
    break;
  case "geocode":
    $count = geocode_remaining($dbs);
    echo_success($count . " stores geocoded.");
    break;
  case "update-training":
    update_training_db($dbt);
    echo_success("Training DB Updated.");
    break;
  case "update-all":
    $count = update_stores_db($dbs);
    update_training_db($dbt);
    echo_success("DB Updated.<br>" . $count . " Stores Geocoded.");
    break;
  case "backup":
    $backupFile = backup_db();
    echo_success("DB Backed Up: <br>" . $backupFile);
    break;
  default:
    //Close PDO Connections
    $dbs = null;
    $dbt = null;
    break;
}
}
}
//if being executed from the shell, update all
else if($totalArgv > 0){
$count = update_stores_db($dbs);
update_training_db($dbt);
echo_success("DB Updated.<br>" . $count . " Stores Geocoded.");
break;
};

//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

//Error handling
function die_with_error($error) {
$return = array(
    "status" => "Failed",
    "data" => $error
);
//Close PDO Connections
$dbs = null;
$dbt = null;
die(json_encode($return));
}

function echo_success($message){
$return = array(
    "status" => "OK",
    "data" => $message
);
$return["json"] = json_encode($return);
echo json_encode($return);
//Close PDO Connections
$dbs = null;
$dbt = null;
}

//Move all files
function move_files(){
try {
    move_file('sfinder/StoreList.txt');
    move_file('sfinder/StoreProductIndex.txt');
    move_file('sfinder/StoreTypeIndex.txt');
    move_file('training/TrainingCustomerList.txt');
}
catch(Exception $e){
    die_with_error($e->getMessage());
}

//sleep(1);
//Return JSON
$return["json"] = json_encode($return);
echo json_encode($return);
}

//Move a file
function move_file($filename){
$source ="/home/busaweb/public_html/b3/" . $filename;
$dest = "/home/busaweb/public_html/adminportal/includes/sfupdate/" . $filename;
if(!copy($source, $dest)){
    throw new Exception("Failed to copy file: " . $filename);
}
else{
    //echo "Successfully moved $filename.<br>";
}
}

//Empty a SQL Table
function empty_table($dbconnection, $tablename){
try{
    $sql = "TRUNCATE TABLE " . $tablename;
    $sth = $dbconnection->prepare($sql);
    //$sth->bindParam(':tablename', $tablename, PDO::PARAM_STR);

    // The row is actually inserted here
    $sth->execute();
    //echo " [+]Table '" . $tablename . "' has been emptied.<br>";
    $sth->closeCursor();
}
catch(PDOException $e) {  
    die_with_error($e->getMessage()); 
}
}

//Import CSV file from JDE to DB
function load_csv($dbconn, $filename, $tablename){
try{
    $sql = "LOAD DATA LOCAL INFILE '/home/busaweb/public_html/adminportal/includes/sfupdate/" . $filename . "' INTO TABLE  " . $tablename . " FIELDS TERMINATED BY  '\\t' ENCLOSED BY  '\"' ESCAPED BY  '\\\' LINES TERMINATED BY  '\
'";
    $sth = $dbconn->prepare($sql);

    // The row is actually inserted here
    $sth->execute();
    //echo " [+]CSV File for '" . $tablename . "' Table Imported.<br>";
    $sth->closeCursor();
}
catch(PDOException $e) {  
    die_with_error($e->getMessage()); 
}
}
function update_stores_db($dbs){
move_files();
empty_table($dbs, "stores");
load_csv($dbs, 'sfinder/StoreList.txt', 'stores');
empty_table($dbs, 'stores_product_type_link');
load_csv($dbs, 'sfinder/StoreProductIndex.txt', 'stores_product_type_link');
empty_table($dbs, 'stores_store_type_link');
load_csv($dbs, 'sfinder/StoreTypeIndex.txt', 'stores_store_type_link');
return $count;
}

function update_training_db($dbt){
move_file('training/TrainingCustomerList.txt');
empty_table($dbt, 'customers');
load_csv($dbt, 'training/TrainingCustomerList.txt', 'customers');
}
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3