2013-02-25 84 views
2

我已經創建了一個窗體,我通過該窗體上傳圖像,我創建了一個XML文件中的節點,我想保存圖像的名稱該文件,所有的東西都在工作,但每當我上傳新的圖片,它只是替換舊的文件名,我想這樣做,如果我上傳一張圖片,那麼它應該自動創建一個節點,然後動態添加圖片名稱取代舊的。如何使重複的XML節點,並保存不同的值,而不覆蓋

形式我使用:

<div id="popup_box_slider_image" class="Add-Social-Media"> 
<!-- OUR PopupBox DIV--> 
<a id="popupBoxClosesliderImage" class="ClosePopup"></a> 
<form id="addEditFormSelectTemplate" action="" method="post" enctype="multipart/form-data" > 
<p>Please upload a image to add to slider.</p> 
<br /> 
<div style="width:100%; float:left;"> 
<br/> 

上傳圖片:

<input type="file" name="sliderImage" id="slider" value="" style="width:180px;" class="field-box" /> 
<br /> 
<span id="sliderImageErr">&nbsp;</span> 
<br/> 
<br/> 
<br/> 
<div align="left"> 
<input type="submit" name="SelectsliderImage" onClick="return validatesliderImage();" value="Update" style="background-color:#2D69A9 ; color:#FFFFFF ; padding-top:5px; padding-bottom:5px; padding-right:10px; padding-left:10px; border:none ; cursor:pointer ; border-radius:5px; " /> 
</div> 
</div> 
</form> 
</div> 

PHP代碼我使用:

$sliderimagename=$_FILES['sliderImage']['name']; 
$xmlpath=SITE_URL."xml/".$_SESSION['username']."/test.xml"; 
$document=simplexml_load_file($xmlpath); 
$document->body->sliderimage = $sliderimagename; 
$document->asXML($xmlpath); 
$path=SITE_URL."/slider_images/"; 
move_uploaded_file($_FILES['sliderImage']['tmp_name'],$path.$sliderimagename); 

XML節點結構:

<body> 
    <title>changeBg</title> 
    <imagename>B4.jpg</imagename> 
    <sliderimage></sliderimage> 
</body> 
+0

如何['加入child'(http://php.net/manual/en /book.simplexml.php)加載到文檔後? – Havelock 2013-02-25 08:18:14

+0

請你說明一下,其實我對它很陌生。我希望你不會介意它。 – 2013-02-25 08:20:23

回答

2

通過使用SimpleXMLElement的addChild方法您想要添加新的子項。 使用當前的XML結構,它會是這個樣子:

//... 
$document=simplexml_load_file($xmlpath); 
$new_image = $document->body->addChild('sliderimage', $_FILES['sliderImage']['name']); 

這將導致一個XML輸出是這樣的:

<body> 
    <title>changeBg</title> 
    <imagename>B4.jpg</imagename> 
    <sliderimage></sliderimage> 
    <sliderimage></sliderimage> <!-- this is the newly created node --> 
</body> 

但你可能會想調整你的XML文件所以你可以有嵌套的一個多層次,例如像這樣:

<body> 
    <file> <!--- new top level tag to group the individual images --> 
     <title>changeBg</title> 
     <imagename>B4.jpg</imagename> 
     <sliderimage>old image</sliderimage> 
    </file> 
    <file> 
     <!--- ... --> 
     <sliderimage>new image</sliderimage> 
    </file> 
</body> 

在這種情況下,你只需要調用addChild多個TI

$new_file = $document->body->addChild('file'); // adding a new <file> node 
$new_file->addChild('title', 'some title'); // adding more nodes inside the new <file> node 
$new_file->addChild('imagename', '...'); 
// ... 
+0

你的是更加充實,應該有更快的礦 - upvote – drk 2013-02-25 08:25:01

+0

@ drk,非常感謝 – complex857 2013-02-25 08:36:18

+0

@ complex857感謝很多隊友,它的工作方式我想要的。 – 2013-02-25 08:58:44

1

http://www.php.net/manual/en/simplexmlelement.addchild.php

添加一個新的子元素:像這樣返回的節點上MES $文檔 - >體佩>的addChild( 'sliderimage',$ sliderimagename);

<body> 
<title>changeBg</title> 
<imagename>B4.jpg</imagename> 
<sliderimage>First</sliderimage> 
<sliderimage>Second</sliderimage> 
</body> 

但是你或許應該改變的XML,表明有一組可用的項目

<body> 
<title>changeBg</title> 
<imagename>B4.jpg</imagename> 
<sliderimages> 
    <image>First</image> 
    <image>Second</image> 
</sliderimages> 
</body> 
相關問題