Escribir en un archivo XML usando fopen en Wordpress

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.

Aquí está el código:

<?
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?

Muchas Gracias

preguntado el 08 de noviembre de 11 a las 15:11

1 Respuestas

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); 
}

respondido 08 nov., 11:19

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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.