2017-06-17 55 views
-1

我得到了一個從目錄到mysql插入腳本的工作映像。問題是我在每個目錄後需要更改腳本本身的名稱從其他目錄插入下一個圖像。每個文件和每個diretory與圖像是在一個名爲「圖像」prent目錄。所以我的問題我怎麼能說從這個目錄中插入一切在MySQL數據庫中,並使用該目錄的名稱作爲其中的圖像的類別名稱。如何提高使用php批量mysql插入從目錄一次抓取所有目錄和圖像

<?php 
$server = 'xxxxx'; 
$dbuser = 'xxxxxx'; 
$dbpass = 'xxxxxx'; 
$dbname = 'xxxxxxx'; 

$connect = mysql_connect($server,$dbuser,$dbpass); 
mysql_select_db($dbname,$connect); 





$path = "images/thanks/"; 
$files = array_map('mysql_real_escape_string',array_filter(glob("{$path}*.*"),'is_file')); 

if(empty($files)){ 
    echo "There were no matching files to insert into the database."; 
} else {  
    $insertValues = array(); 
foreach($files as $file) 

{ 
$data = getimagesize($file); 
$width = $data[0]; 
$height = $data[1]; 
    $insertValues[] = "('Titel', 'thanks', '{$file}', '$width', '$height')"; 
}$query = "INSERT INTO `gbpics` (`gbpictitel`, `gbpiccat`, `gbpicurl`, `gbpicwidth`, `gbpicheight`) VALUES " . implode(', ', $insertValues); 
    if(!mysql_query($query)){ 
     echo "There was a problem inserting the data."; 
     trigger_error("Query failed: $query<br />Error: " . mysql_error()); 
    } else { 
     echo "The data was inserted successfully."; 
    } 
}?> 
+0

我強烈建議你在代碼審查第一張貼此。 – hakre

回答

0

試試這個:

<?php 
$server = 'xxxxx'; 
$dbuser = 'xxxxxx'; 
$dbpass = 'xxxxxx'; 
$dbname = 'xxxxxxx'; 

$connect = mysql_connect($server, $dbuser, $dbpass); 
mysql_select_db($dbname, $connect); 




$dirs = array_filter(glob('images/*'), 'is_dir'); 
foreach ($dirs as $dir) { 
    $path = "images/" . $dir . "/"; 
    $files = array_map('mysql_real_escape_string', array_filter(glob("{$path}*.*"), 'is_file')); 

    if (empty($files)) { 
     echo "There were no matching files to insert into the database."; 
    } else { 
     $insertValues = array(); 
     foreach ($files as $file) { 
      $data   = getimagesize($file); 
      $width   = $data[0]; 
      $height   = $data[1]; 
      $insertValues[] = "('Titel', {$dir}, '{$file}', '$width', '$height')"; 
     } 
     $query = "INSERT INTO `gbpics` (`gbpictitel`, `gbpiccat`, `gbpicurl`, `gbpicwidth`, `gbpicheight`) VALUES " . implode(', ', $insertValues); 
     if (!mysql_query($query)) { 
      echo "There was a problem inserting the data."; 
      trigger_error("Query failed: $query<br />Error: " . mysql_error()); 
     } else { 
      echo "The data was inserted successfully."; 
     } 
    } 
} 
?> 

順便說一句,你已經很過時這裏的代碼... 不使用mysql_使用mysqli_

+0

嗨,朋友是的,我會建議,但腳本沒有工作,他給了我這樣的錯誤千次「沒有匹配的文件插入到數據庫中。」有一個想法?我的上傳文件在根目錄我的分類文件夾中的主文件夾圖像,所以它應該工作不能找出爲什麼不.. ..請幫助@RiazLaskar –

相關問題