2014-02-20 207 views
0

我想创建一个xml文件并在其中写入代码。但事情是,我必须将它切成三段,因为代码的中间部分是循环的。到目前为止,这是我的代码看起来像。创建一个xml文件

编辑

这是输出我想

<download serverurl='http://localhost/files'> 
    <file filename='image1.PNG' md5='b45110dd47aa28f8400295ec70e6b0ef'/> 
    <file filename='image2.jpg' md5='3a3b3f573682ab177939b748c4623db0'/> 
    <file filename='image3.jpg' md5='deaee69864bedcd5c60af3bb46b47edc'/> 

我总是收到。

  <file filename='image2.jpg' md5='3a3b3f573682ab177939b748c4623db0'/> 
    <file filename='image3.jpg' md5='deaee69864bedcd5c60af3bb46b47edc'/> 
<download serverurl='http://localhost/files'>    
<file filename='image1.PNG' md5='b45110dd47aa28f8400295ec70e6b0ef'/> 
</download> 

我得到的输出是非常随机的。

END EDIT

public function download_package1(){ 
    $form = $this->getUserData(); 

    $serverurl = "<download serverurl='http://222.127.182.57/file'>"; 

    file_put_contents('download.xml', $serverurl, FILE_APPEND); 
} 

public function download_package_images ($filename){ 
    $form = $this->getUserData(); 

    $images = " 
    <file filename='".$filename."' md5='".md5_file($filename)."'/>"; 

    file_put_contents('download.xml', $images, FILE_APPEND); 

    //THIS IS FUNCTION IS IN LOOP. THERE ARE THE FILENAMES OF THE IMAGES UPLOADED 
} 

public function download_package_end(){ 
    $form = $this->getUserData(); 

    $end = " 
</download>"; 

    file_put_contents('download.xml', $end, FILE_APPEND); 

,但我的顺序不是固定的。我已经尝试了fopen/fopen,但同样的事情发生。我真的不知道我怎么能解释这一点,但我希望我有道理。如果任何人都可以请帮助我,我会非常感激。谢谢。

+0

你可以发布您收到并导致您所期待的结果? – Tamara

+0

编辑我的问题@Tamara – user3026876

回答

0

而不是建立手工XML,使用XML解析器:

$xml = simplexml_load_string("<download />"); 
$xml->addAttribute('serverurl', 'http://localhost/files'); 

for ($i = 0;$i < 3; $i++) 
{ 
    $file = $xml->addChild('file'); 
    $file->addAttribute('filename', "image$i.png"); 
    $file->addAttribute('md5', 'whateverhash');  
} 

$xml->asXML('filename.xml'); 

看到它的工作:https://eval.in/104654