2016-06-04 2 views
1

이 작동이미지를 업로드하는 더 나은 방법?

$Product_images = $_FILES['product_img']; 

foreach($Product_images['name'] as $key => $value) { 

    $file_name = iSQLsecure($objConnection, $value); // iSQLsecure is my own custom function 
    $file_name = trim($file_name); 
    $file_tmp = $Product_images['tmp_name'][$key]; 
    $file_size = $Product_images['size'][$key]; 

    $file_ext = explode('.', $file_name); 
    $file_ext = strtolower(end($file_ext)); 

    $file_trim_name = rtrim($file_name, ".".$file_ext); 
    $file_name_new = uniqid($file_trim_name . "-", true) . '.' . $file_ext; 

    $file_destination = 'img/products/' . $file_name_new; 
    $image_alt = str_replace("img/products/", "", $file_destination); 

    move_uploaded_file($file_tmp, "../" . $file_destination); 

    $query_images = "INSERT INTO images (image_path, image_alt, fk_product_id) 

    VALUES 

    ('{$file_destination}', '{$image_alt}', '{$new_id}')"; 
    $objConnection->query($query_images) or die($objConnection->error); 

} 

PHP,하지만 내 생각에, 나는 한 시간보다 $query_images 이상을 실행하지 않아야합니다. 업로드 한 모든 이미지를 반복 할 수있는 방법이 있어야하지만 번만 실행해야합니까?

답변

1

예, 있습니다. 배열을 사용하여 각 삽입 값 세트를 추가하고 루프 다음에 단일 쿼리 문자열을 작성한 다음 한 번 실행하십시오.

$Product_images = $_FILES['product_img']; 

$sql_insert = array(); 

foreach($Product_images['name'] as $key => $value) { 

    // ... Previous code until $query_images = ... 

    $sql_insert[] = "('{$file_destination}', '{$image_alt}', '{$new_id}')"; 
} 

$query_images = "INSERT INTO images (image_path, image_alt, fk_product_id) 
       VALUES " . implode(',', $sql_insert); 
$objConnection->query($query_images) or die($objConnection->error); 
+0

감사합니다. 오랫동안 코딩하지 않았으므로 모든 것을 기억하기가 어렵습니다. 행복 할 수있는 사람들이 있습니다. 정말로 도움을 주셔서 감사합니다. – Simon

+0

당신은 환영합니다 :) – smozgur