簡體   English   中英

PHP CSV - MYSQL Timing out

[英]PHP CSV - MYSQL Timing out

首先,我搜索了相關的答案,有一些 - 但找不到任何與我想要的相關的東西。 我相信有更好的方法!

基本上,我正在尋找一些建議,我有一個腳本,其中將服務器上的CSV文件轉換為MYSQL數據庫。 但是,CSV文件是20mb,腳本只是超時。 我修改了我的php.ini所以它不應該超時,但它會導致500內部服務器錯誤。 有沒有辦法可以讓頁面保持活動狀態而不是超時(jquery會做這樣的事情) - 如果沒有,我知道我過去曾經使用類似shell執行大型SQL文件,是否可以選擇使用類似的東西這個? 建議真的很感激。 我的編程知識真的太好了,所以請耐心等待

這是我正在使用的腳本:

<?php 
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername ="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/


if(!file_exists($csvfile)) {
      echo "File not found. Make sure you specified the correct path.\n";
      exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
      echo "Error opening data file.\n";
      exit;
}

$size = filesize($csvfile);

if(!$size) {
      echo "File is empty.\n";
      exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());

$lines = 0;
$queries = "";
$linearray = array();

foreach(split($lineseparator,$csvcontent) as $line) {

      $lines++;

      $line = trim($line," \t");

      $line = str_replace("\r","",$line);

      /************************************
      This line escapes the special character. remove it if entries are already escaped in the csv file
      ************************************/
      $line = str_replace("'","\'",$line);
      /*************************************/

      $linearray = explode($fieldseparator,$line);

      $linemysql = implode("','",$linearray);

      if($addauto)
            $query = "insert into $databasetable values('','$linemysql');";
      else
            $query = "insert into $databasetable values('$linemysql');";

      $queries .= $query . "\n";

      @mysql_query($query);
}

@mysql_close($con);

if($save) {

      if(!is_writable($outputfile)) {
            echo "File is not writable, check permissions.\n";
      }

      else {
            $file2 = fopen($outputfile,"w");

            if(!$file2) {
                  echo "Error writing to the output file.\n";
            }
            else {
                  fwrite($file2,$queries);
                  fclose($file2);
            }
      }

}

echo "Found a total of $lines records in this csv file.\n";


?>

您應該能夠使用LOAD DATA INFILE在一個MySQL命令中執行此操作,而不是手動處理csv。 這將把處理轉移到MySQL,並且應該意味着PHP不會超時。 如果它仍然超時,你總是可以從shell運行相同的命令。

不要解析自己的CSV

使用PHP的內置函數fgetcsv()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM