2013-07-12 5 views
0

끌어서 놓기 업 로더를 수정하려고합니다. 파일이 업로드되면 오른쪽에 표시됩니다. 오른쪽에 각 파일에 대한 설명 필드를 추가하고 싶습니다. 내 XML 디렉토리를 확인하여 작성되고 있습니다. 각 파일의 설명을 XML에 저장할 수 있습니다. 그러나 디렉토리에 파일을 제거하거나 업로드하는 경우 모든 설명을 "-"으로 덮어 씁니다. 내가하려고하는 일은 이전 항목 설명을 덮어 쓰지 않고 새 항목을 업로드하거나 제거하는 것입니다.선택한 노드가 업데이트되지 않습니다.

PHP- 제거 및 업로드.이 함수는 업로드 된 각 파일에 대해 디렉토리를 확인하고 XML에 노드를 추가합니다. 각 항목에 대한 설명을 추가

if ($handle = opendir($path_to_image_dir)) 
{ 
    while (false !== ($file = readdir($handle))) 
    { 
     if (is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store") 
     { 
      list($width, $height) = getimagesize($path_to_image_dir.'/'.$file); 
      $image = $xml_generator->addChild('image'); 
      $image->addChild('id', 'file'.$i++); 
      $image->addChild('name', $file); 
      $image->addChild('width', $width); 
      $image->addChild('height', $height); 
      $image->addChild('description', '-'); 
     } 
    } 
    closedir($handle); 
} 

// Write the XML File to a file. 
$xml_generator->asXML('../file.xml'); 

Description.Php.

$filename = "../file.xml"; 

//value form the pop up description form. 
$description = $_POST['description']; 

// id of input from the write rightside list. 
$hid = $_POST['fileID']; 

$xml->image ->description = $description; 

$xml = simplexml_load_file($filename); 

foreach($xml->image as $image) 
{ 
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID) 
    { 
     $image->description = $description; 
    } 
} 

$xml->asXML($filename); 

내가 먼저 당신이, 당신의 Description.Php로 3 문제를 볼 수 있습니다

<images> 
    <image> 
    <id>file0</id> 
    <name>image1.jpg</name> 
    <height>1435</height> 
    <width>2551</width> 
    <description>-</description> 
    </image> 
    <image> 
    <id>file1</id> 
    <name>Image2.jpg</name> 
    <height>1435</height> 
    <width>2551</width> 
    <description>-</description> 
    </image> 
</images> 
+1

'echo $ image-> id;'와'echo $ description;'는 ** Description.Php **로 무엇을 제공합니까? 그들은 표시합니까? --- 질문에있는 코드가 잘못 보이지 않기 때문에 묻는 중입니다. 예제를 압축 할 수 있다면 파일 상호 작용없이 간단히 실행하면 문제를 개선하는 데 도움이됩니다. 결국 그것은 XML에 관한 것이지 파일 업로드가 아니기 때문에 제목을 사용하면 일부 사용자가 이탈 할 수도 있습니다. – hakre

+0

echo $ image-> id; 그리고 echo $ description; 나를 위해 콘솔 로그에 아약스 요청에 값을 잡아입니다. 그것들은 무시할 수 있습니다. 그것은 실제로 아무 것도하지 않습니다. 미안해, 너를 혼란스럽게 할거야. 너는 제목과 질문에 대해 옳을 수도있다. 감사! – Tierendu

+0

@Prix file0은 내 HTML 입력의 ID입니다. 그 다음 xml id-> file0과 일치시킵니다. 따라서 xml의 유일한 file0 섹션을 업데이트합니다. 이 부분은 이미 나를 위해 작동합니다. – Tierendu

답변

0

XML $fileID라고, 당신은 또한 simplexml_load_file 전에 $xml->image ->description = $description;를 호출하고 마지막 했어야 변수 $hid fileID와 설명이 설정되어 있는지 확인하지 마십시오.

그래서 파일은 다음과 같이해야한다 : 또한 POST가 존재한다는 것을 보장하기 위해 isset 또는 이와 유사한을 사용해야합니다

$filename = "../file.xml"; 

//value form the pop up description form. 
$description = $_POST['description']; 
if (!isset($description)) 
    die("The field description is not set."); 

// id of input from the write rightside list. 
$fileID = $_POST['fileID']; 
if (!isset($fileID)) 
    die("The field fileID is not set."); 

$xml = simplexml_load_file($filename); 

foreach($xml->image as $image) 
{ 
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID) 
    { 
     $image->description = $description; 
    } 
} 

$xml->asXML($filename); 

.

첫 번째 코드와 관련하여 XML을 읽지 않고도 각 파일에 대한 새로운 노드를 반복해서 만들고 이전 XML을 덮어 쓰는 새로운 XML로 저장하면 이전 설명을 유지해야합니다.

if ($handle = opendir($path_to_image_dir)) 
{ 
    while (false !== ($file = readdir($handle))) 
    { 
     if (is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store") 
     { 
      $fileID = 'file'.$i++; 
      list($width, $height) = getimagesize($path_to_image_dir.'/'.$file); 
      $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]'); 
      if (count($oldImage) == 0) 
      { 
       $image = $xml_generator->addChild('image'); 
       $image->addChild('id', $fileID); 
       $image->addChild('name', $file); 
       $image->addChild('width', $width); 
       $image->addChild('height', $height); 
       $image->addChild('description', '-'); 
      } 
      else 
      { 
       $oldImage = $oldImage[0]; 
       $oldImage->name = $file; 
       $oldImage->width = $width; 
       $oldImage->height = $height; 
      } 
     } 
    } 
    closedir($handle); 
} 

// Write the XML File to a file. 
//$xml_generator->asXML('../file.xml'); 
//In case it saves all in one line and u want 
//to properly format the XML use: 
$dom = new DOMDocument('1.0'); 
$dom->preserveWhiteSpace = false; 
$dom->formatOutput = true; 
$dom->loadXML($xml_generator->asXML()); 
echo $dom->save('../file.xml'); 
+0

아, 내 문제는 구문 문제였습니다. '$ fileID'로 변경하려고하는 예제를 게시 할 때 변수의 일부를 변경했지만 실제 코드의 두 위치 모두에서 동일한 변수를가집니다. 나는 당신의 코드를 시험해 보았다. 그것은 제 코드처럼 작동하지만, 여러분의 길은 훨씬 더 깨끗합니다. 그냥 설명을 바꾸면 잘 작동합니다. 그러나 그것은 내 초기 문제를 해결하지 않습니다. 파일을 업로드하거나 수정 (제거) 할 때 여전히 덮어 씁니다. 모든 설명은 ' -'으로 바뀝니다. 나는 당신의 도움에 정말로 감사한다 – Tierendu

+0

@Tierendu는 $ xml_generator가'../ file.xml'을 읽는 것을 감안할 때 업데이트를 보았습니다. 그런 다음 우리는 그것을 기존의 이미지와 일치시키기 위해 사용할 수 있고 빈 파일로 전체 파일을 재생성하는 대신 간단히 업데이트 할 수 있습니다. – Prix

+0

나는 그 문제에 대한 느낌이 있었고 정말로이 문제를 해결할 게임 계획을 세울 수 없었다. 감사! – Tierendu

관련 문제