1

I'm trying to write to an XML file in the uploads folder in my Wordpress directory. This XML needs to update each time the client updates or creates a new post using a custom post_type I created.

Here is the code:

<?
add_action( 'save_post', 'producers_xml' );

function producers_xml(){

 if ($_POST['post_type'] == 'producer') 
 {
   $xml = new SimpleXMLElement('<xml/>');
   $producers = get_posts( array( 'post_type'=>'producer', 'numberposts'=>-1 ) );

   $xml->addChild('producers');

   foreach($producers as $i=>$producer){
     $name = get_the_title($producer->ID);
     $owner = get_post_meta($producer->ID, '_producer_contact_name', true);
     $phone = get_post_meta($producer->ID, '_producer_phone', true);
     $fax = get_post_meta($producer->ID, '_producer_fax', true);
     $email = get_post_meta($producer->ID, '_producer_email', true);
     $website = get_post_meta($producer->ID, '_producer_website', true);
     $address = get_post_meta($producer->ID, '_producer_address', true);

     $xml->producers->addChild('producer');
     $xml->producers->producer[$i]->addChild('name', $name);
     $xml->producers->producer[$i]->addChild('owner', $owner);
     $xml->producers->producer[$i]->addChild('phone', $phone);
     $xml->producers->producer[$i]->addChild('fax', $fax);
     $xml->producers->producer[$i]->addChild('email', $email);
     $xml->producers->producer[$i]->addChild('website', $website);
     $xml->producers->producer[$i]->addChild('address', $address); 
 }

 $file = '../../../uploads/producers.xml';
 $open = fopen($file, 'w') or die ("File cannot be opened.");
 fwrite($open, $xml->asXML());
 fclose($open); 
}

} ?>

The problem I am having is that it keeps giving me the "File cannot be opened." error that I supplied. My uploads folder (and all enclosed items) have full permissions (777). I've tested my code locally and it works, but I can't get it to open that file on the remote server. I don't have root access to it so I can't change permissions on anything before httpdocs.

I should also mention that fopen is enabled on the server.

EDIT: allow_url_fopen is enabled but allow_url_include is disabled.

Anybody know what my problem is?

Thanks

1 Answer 1

2

Try using an absolute path (full path), alongside other checks see:

 $file = 'home/my/path/uploads/producers.xml'; //Absolute path
if(is_file($file) && is_readable($file)){
 $open = fopen($file, 'w') or die ("File cannot be opened.");
 fwrite($open, $xml->asXML());
 fclose($open); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked. Funny how the solution seems so simple in hindsight. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.