PHP文件不再拉入.txt文件了

时间:2015-05-19 19:37:28

标签: php mysql

我有一个func.php文件,它从我的服务器中获取三个.txt文件,并将数据输入到MySQL数据库的表中。我最近将我的PHP从5.3升级到5.4,这导致了一个问题,即它不再引入.txt文件,只是删除了表。在update_training_db函数中,它运行empty_table但不运行move_fileload_csv。这段代码在5.3中工作,但我不确定为什么它不再抓取文本文件。有人可以帮忙吗?

整个PHP脚本由4个函数组成,如整个代码所示。 empty_table,move_file,load_csv和update_training_db来执行所有操作。

enter image description here

问题:

function update_training_db($dbt){
    empty_table($dbt, 'customers');
    move_file('/training/TrainingCustomerList.txt');
    load_csv($dbt, '/training/TrainingCustomerList.txt', 'customers');
}

整个文件:

<?php
header('Content-Type: application/json; charset=UTF-8');
$argv = $_SERVER['argv'];
$totalArgv = count($argv);

// Use PDO to connect to the DB
$dsn_training = 'mysql:dbname=training;host=127.0.0.1';
$user = 'training';
$password = 'training';

try {
    $dbt = new PDO($dsn_training, $user, $password);
    $dbt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo 'Connected to SQL Server';
} 

function empty_table($dbconn, $tablename){
    try{
        $sql = "TRUNCATE TABLE " . $tablename;
        $sth = $dbconn->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()); 
    }
}

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

function move_files(){
    try {
        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);
}

function load_csv($dbconn, $filename, $tablename){
    try{
        $sql = "LOAD DATA LOCAL INFILE '/includes/sfupdate/" . $filename . "' INTO TABLE  " . $tablename . " FIELDS TERMINATED BY  '\\t' ENCLOSED BY  '\"' ESCAPED BY  '\\\' LINES TERMINATED BY  '\\n'";
        $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_training_db($dbt){
    empty_table($dbt, 'customers');
    move_file('/training/TrainingCustomerList.txt');
    load_csv($dbt, '/training/TrainingCustomerList.txt', 'customers');
}

0 个答案:

没有答案