2

I am using the unlink() method to the delete the file, but I think my syntax to get the string from my XML file is applying the wrong value. The file still remains undeleted but the good news is the script returns true and still removes the post from the XML.

My HTML form blog.php sends the $_POST["CHECK"] value:

<html>
<body>

<form method="post" action="remove_post.php">
   <input type="hidden" name="CHECK" value="pst_02-02-2014_02:00:00pm"  />
   <input type="submit" name="CLOSE" value="Delete Post"  />
</form>

</body>
</html>

My XML file: data.xml

<?xml version="1.0" encoding="UTF-8"?>
<blog>
    <posting id="pst_01-01-2014_01:00:00pm">
        <date>01-01-2014</date>
        <time>01:00:00pm</time>
        <title>Coming!</title>
        <content>Blog Posts soon!</content>
    </posting>
    <posting id="pst_02-02-2014_02:00:00pm">
        <date>02-02-2014</date>
        <time>02:00:00pm</time>
        <title>A Blog!</title>
        <content>Blog Posts coming soon!</content>
        <image>thumb.jpg</image>
    </posting>
</blog>

My PHP file: remove_post.php

<?php
if ( isset( $_POST["CLOSE"] ) )
   {
   $check = $_POST["CHECK"] ;

   $doc = new DOMDocument() ;
   $doc -> load( "data.xml" ) ;
   $xpath = new DOMXPath( $doc ) ;
   $post_element = $xpath -> query( "/blog/posting[@id='$check']" ) ;

   $image_element = $xpath -> query( "/blog/posting[@id='$check']/image" ) ; // Suspected problem
   $image = "blog_images/" . $image_element -> firstChild ; // Suspected problem

   foreach ( $post_element as $post )
      {
      unlink( $image ) ;
      $post -> parentNode -> removeChild( $post ) ;
      }

   $doc -> save( "data.xml" ) ;
   header( "Location: blog.php" ) ;
   }
?>

...and yes I have checked my file permissions:

Folder (owner) permissions: /blog_images/

  • read
  • write
  • execute

Folder (public) permissions:

  • read
  • execute

File (owner) permissions: thumb.jpg

  • read
  • write

File (public) Permissions:

  • read
Kevin
  • 41,694
  • 12
  • 53
  • 70
Xavier
  • 109
  • 9
  • Try using the full absolute path `/blog_images/` or perhaps `./blog_images/` – l'L'l Sep 17 '14 at 02:44
  • @l'L'l Tried both and no go. To be noted that my php form is under XHTML DOCtype. – Xavier Sep 17 '14 at 02:53
  • @l'L'l Also I get `return false` when I use full paths `http://example.com/goes.php` in my PHP. – Xavier Sep 17 '14 at 02:56
  • By full path I mean something such as `/var/www/website/public/blog_images/` – l'L'l Sep 17 '14 at 03:04
  • @l'L'l So like this? `$image = "website.com/blog_images/" . $image_element -> firstChild ;` – Xavier Sep 17 '14 at 03:06
  • are getting the proper node? `$image_element` does this contain the value? – Kevin Sep 17 '14 at 03:07
  • @l'L'l Via XPath - `$image_element = $xpath -> query( "/blog/posting[@id='$check']/image" ) ;` – Xavier Sep 17 '14 at 03:09
  • @Xavier, No, the absolute path would be the one reflected when viewing it on your webserver. So for example, if logged in through ssh and you're in the directory `/blog_images/` you could get the absolute path by using `pwd`. You can also get the path through php (see here: http://stackoverflow.com/a/10417279/499581) – l'L'l Sep 17 '14 at 03:09
  • @l'L'l I used this code to make the file so should it not be the same path to remove? `move_uploaded_file( "$temp_file , /blog_images/thumb.jpg" ) ` – Xavier Sep 17 '14 at 03:25
  • I couldn't tell you without seeing the function `move_uploaded_file`. Try using php example I linked by saving it (temporarily) as `cwd.php` in `/blog_images/` then view it. You'll see the absolute path. – l'L'l Sep 17 '14 at 03:35
  • @l'L'l Thanks for your help. The answer below was my problem. Using the `item()` method was the solution. – Xavier Sep 17 '14 at 03:53
  • @Xavier, Sure thing — glad you found the issue. :) – l'L'l Sep 17 '14 at 04:25

1 Answers1

1

Provided the element exists, after getting the xpath value, it returns a DOMNodeList, so that means you have to access it first:

if($image_element->length > 0) { // if it exists
    $image_name = $image_element->item(0)->nodeValue;
                                  // ^ directly access it if you're expecting one value
    // thumbs.jpg
}

Or also you can loop it:

foreach($image_element as $e) {
    echo $e->nodeValue . '<br/>';
}

Now for unlinking:

unlink('blog_images/' . $image_name); // or you can add a file_exists() there just to be sure
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • It worked! Thanks very much. That is the correct way to target an element in XML using PHP. – Xavier Sep 17 '14 at 03:51
  • 1
    Adding this code and removing this code `$image_element = $xpath -> query( "/blog/posting[@id='$check']/image" ) ; $image = "blog_images/" . $image_element -> firstChild ;` was the solution. – Xavier Sep 17 '14 at 03:56