2013-07-24 2 views
0

재고 목록에 자동차 목록이 있습니다. 각각 최대 12 장의 사진을 가질 수 있습니다. 내가 원한다면 나는 성공적으로 차를 지울 수있다, 내가 가지고있는 문제는 사진을 지우는 것이다. 내가 그렇게 echo $data['picture1'];PHP는 for 루프의 파일을 지 웁니다.

내가 루프 루프를 위해 만들려고 노력하는 것 같은 사진의 이름을 액세스 할 수 있으며, $data 변수로

$pictures = "picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9, picture10, picture11, picture12"; 
$data = mysql_fetch_assoc(mysql_query("SELECT $pictures FROM `auto` WHERE `auto_id` = $autoid")); 

:

그래서 나는 이런 식으로 뭔가를 조회 할 수 있습니다 각 사진, db의 필드가 비어 있지 않은 한 데이터에서 해당 파일 경로의 배열을 만들고 배열을 통해 루프를 지우려면 unlink()을 사용 하시겠습니까?

SO FAR 솔루션 [작동하지만 개선이 필요한?]

if (isset($_POST['delete']) === true) 
{ 

    $pictures = "picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9, 
    picture10, picture11, picture12"; 
    $data = mysql_fetch_assoc(mysql_query("SELECT $pictures FROM `auto` WHERE `auto_id` = $autoid")); 
    $a = 1; 
    while ($a <= 12) 
    { 
     $picturepath = $data['picture'.$a]; 
     if (empty($picturepath) !== true) 
     { 
      unlink('../../' . $picturepath); 
     } 
     $a++; 
    } 

    mysql_query("DELETE FROM `auto` WHERE `auto_id` = $autoid"); 
    header('Location: ../../admin.php?manage_vehicles'); 
    exit(); 
} 
+0

필드 (열) PICTURE1, pcture2 등 그림 파일의 경로 이름을 포함? –

+0

내 솔루션에 나쁜 PHP가 포함되어 있습니까? –

답변

0

을 추가했다.

foreach ($data as $picname) { 
    if (file_exists($picname) && is_file($picname)) unlink($picname); 
} 

쉽게 : 당신은 단순히 뭔가를 할 수있을 때 왜하지만 귀찮게.

괜찮아 보자. 사소한 일이지만, 대신 isset보다는 array_key_exists를 사용하는 편이 좋다. foreach를 사용하지 않는다면 for 루프를 사용하고 그림 [n] 열을 지정해야하는 번거 로움을 피할 수있다. 쿼리 문자열 그래서 방법에 대해 : 내가 가져

if (array_key_exists('delete',$_POST)) 
{ 
    // not needed 
    // $pictures = "picture1, picture2, picture3, picture4, picture5, picture6, picture7, 
    // picture8, picture9, picture10, picture11, picture12"; 
    // if $autoid is an integer, you can reduce the risk of sql injection by intval() 
    // otherwise look at using preg_replace or using mysqli functions with prepare and bind_params 
    $autoid=intval($autoid); 
    $res=mysql_query("SELECT * FROM `auto` WHERE `auto_id` = $autoid")); 
    if (!($res===false)) 
    { 
     $data = mysql_fetch_assoc($res); 
     if (!($data===false)) 
     { 
     for ($i=0; $i<=12; $i++) 
     { 
      $pic_col="picture$i"; 
      if (!array_key_exists("$pic_col",$data) || !strcmp(trim($data["$pic_col"]))) continue; 
      $picname = '../../'.trim($data["$pic_col"]); 
      if (file_exists($picname) && is_file($picname)) unlink($picname); 
     } 
     } 
    } 
    mysql_query("DELETE FROM `auto` WHERE `auto_id` = $autoid"); 
    header('Location: ../../admin.php?manage_vehicles'); 
    exit(); 
} 
+0

정말 고맙습니다. 실제로 조금 다릅니다. 새 코드를 확인하십시오. 내 대답을 내 작업 솔루션과 결합하는 것에 대한 제안입니다. @ArthurNicoll –

0

는 조회 후 foreach 루프를 추가합니다. 대답 업데이트

foreach($data AS $photo) 
    { 
     if (!empty($photo) && file_exists($photo)) unlink($photo); 
    } 

는, 그런 다음 $ 데이터 [] 요소에 액세스하는 것을 사용하는 캐릭터 '그림'에 루프 인덱스 값을 추가, 루프를 할 수 file_exists()

+0

덕분에 많은 사람 :) –

+0

나는 $ 사진이 사진의 URL이 될 것이라고 가정합니다. 파일 존재 여부를 확인하기 위해 file_exists()를 사용하고 싶을 수도 있습니다. –

관련 문제