2016-12-16 40 views
0

我在PHP中的新手程序員,我有一個問題..上傳SQL轉儲文件通過PHP錯誤

我嘗試使用PHP上傳SQL轉儲文件(* .SQL)到數據庫中,但發生錯誤..這裏是我的代碼:

<?php 

$eror  = false; 
$folder  = './upload/'; 

$file_type = array('sql'); 
$max_size = 1000000; // 1MB 

if(isset($_POST['btnUpload'])){ 
//Mulai memorises data 
$file_name = $_FILES['data_upload']['name']; 
$file_size = $_FILES['data_upload']['size']; 
//cari extensi file dengan menggunakan fungsi explode 
$explode = explode('.',$file_name); 
$extensi = $explode[count($explode)-1]; 

//check apakah type file sudah sesuai 
if(!in_array($extensi,$file_type)){ 
    $eror = true; 
    $pesan .= '- Type file yang anda upload tidak sesuai<br />'; 
} 
if($file_size > $max_size){ 
    $eror = true; 
    $pesan .= '- Ukuran file melebihi batas maximum<br />'; 
} 
//check ukuran file apakah sudah sesuai 

if($eror == true){ 
    echo '<div id="eror">'.$pesan.'</div>'; 
} 
else{ 
    //mulai memproses upload file 
    if(move_uploaded_file($_FILES['data_upload']['tmp_name'], $folder.$file_name)){ 
     //catat nama file ke database 
      $f = fopen($file_name,"r+"); 
      $sqlFile = fread($f, filesize($file_name)); 
      $sqlArray = explode(';',$sqlFile); 
      foreach ($sqlArray as $stmt) { 
       if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') { 
       $result = mysql_query($stmt); 
       if (!$result) { 
        $sqlErrorCode = mysql_errno(); 
        $sqlErrorText = mysql_error(); 
        $sqlStmt = $stmt; 
        break; 
       } 
       } 
      } 
      if ($sqlErrorCode == 0) { 
       echo "Script is executed succesfully!"; 
      } else { 
       echo "An error occured during installation!<br/>"; 
       echo "Error code: $sqlErrorCode<br/>"; 
       echo "Error text: $sqlErrorText<br/>"; 
       echo "Statement:<br/> $sqlStmt<br/>"; 
      } 
      } 
    } 
    } 
?> 

,它顯示和錯誤是這樣的:

Warning: fopen(test.sql): failed to open stream: No such file or directory in C:\xampp\htdocs\tampung\action_upload.php on line 36 

Warning: filesize(): stat failed for test.sql in C:\xampp\htdocs\tampung\action_upload.php on line 37 

Warning: fread() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\tampung\action_upload.php on line 37 

Notice: Undefined variable: sqlErrorCode in C:\xampp\htdocs\tampung\action_upload.php on line 50 
Script is executed succesfully! 

你能告訴我什麼是錯的?

問候

+0

這無關與MySQL。這純粹是一個PHP文件上傳錯誤。但是你不應該以這種方式導入SQL數據。你應該直接在mysql控制檯中。 – e4c5

+0

問題與閱讀文件,因爲我可以看到錯誤。 – Manwal

回答

0

你必須給移動的文件的完整路徑:

$f = fopen($folder.$file_name,"r+"); 
$sqlFile = fread($f, filesize($folder.$file_name)); 
+0

謝謝先生..它的工作原理... –