SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem using fwrite on an XML file

    I have the following XML file called images.xml
    Code:
    <gallery timer="6" order="sequential" fadetime="3" looping="yes" xpos="0" ypos="0">
    
    <image path="../flash/images/TEN_2.jpg" />
    
    <image path="../flash/images/TEN_3.jpg" />
    
    </gallery>
    I want to be able to add another another image path to this file. So I wrote the following PHP code which is suppose to write another image path to the file.

    PHP Code:
    $file_contents file_get_contents($_SERVER['DOCUMENT_ROOT']."/flash/images.xml");

    $file_contents str_replace '</gallery>'''$file_contents); // delete gallery tag

    $file_contents .= "<image path=\"../flash/images/$new_filename\" /></gallery>"// Add new image path...then close with gallery tag
                
    $fp fopen($_SERVER['DOCUMENT_ROOT']."/flash/images.xml"'w');  /// Open images.xml file

    if(!$fp){
       
    admin_top("Error"$user);
       echo 
    "There has been an error opening the flash/images.xml file.";
       
    admin_footer();
       exit();
    }
            
    fwrite($fp$file_contents); // Write to XML file
    fclose($fp);  /// close file 
    The problem is that the new image path does not write to the file correctly. This is what the XML file will look like after I run the script:

    Code:
    <gallery timer="6" order="sequential" fadetime="3" looping="yes" xpos="0" ypos="0">
    
    <image path="../flash/images/TEN_2.jpg" />
    
    <image path="../flash/images/TEN_3.jpg" />
    
    
    <image pat
    I narrowed the problem down to the quotes at this line:
    PHP Code:
    $file_contents .= "<image path=\"../flash/images/$new_filename\" /></gallery>"
    If I take the quotes away like the following:

    PHP Code:
    $file_contents .= "<image path=../flash/images/$new_filename /></gallery>"
    Then the script will work correctly and the image path will write to the file but with no quotes so the Flash script will not work.

    So the problem comes down to the quotes causing the problem. Anyone know a way to get around this problem...or know a better way to write the script?

  2. #2
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem has been fixed.

    All I did was make the following change and it worked.

    PHP Code:
    $file_contents .= "<image path=\"".SERVER."/flash/images/$new_filename\" />"// Add new image path...then
                
    $file_contents .= "</gallery>"// Close with gallery tag 

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Austin
    Posts
    401
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Have you tried using a a single instead of double quote:
    PHP Code:
    $file_contents .= '<image path="../flash/images/$new_filename" /></gallery>'
    Merchant Equipment Store - Merchant Services, POS, Equipment, and supplies.
    Merchant Account Blog | Ecommerce Blog

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Look at your line again, and try to imagine how someone that was stupid would read it, thats hos the PHP interpreter sees it.

    PHP Code:
    $file_contents .= "<image path=\"../flash/images/$new_filename\" /></gallery>"
    Youve told it that everything within " " should be appended to $file_contents. So when it gets to the path=\" bit it stops and doesnt start again till it sees the next " " pair.

    Change your line to

    PHP Code:
    $file_contents .= '<image path=\"../flash/images/$new_filename\" /></gallery>'
    And try again
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •