普通网友 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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么