【问题标题】:Write to XML file using fopen in Wordpress在 Wordpress 中使用 fopen 写入 XML 文件
【发布时间】:2011-12-24 13:10:39
【问题描述】:

我正在尝试写入我的 Wordpress 目录的上传文件夹中的 XML 文件。每次客户端使用我创建的自定义 post_type 更新或创建新帖子时,此 XML 都需要更新。

代码如下:

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

} ?>

我遇到的问题是它一直给我“文件无法打开”。我提供的错误。我的上传文件夹(以及所有随附的项目)具有完全权限 (777)。我已经在本地测试了我的代码并且它可以工作,但是我无法让它在远程服务器上打开该文件。我没有对它的 root 访问权限,因此我无法在 httpdocs 之前更改任何内容的权限。

我还要提一下,服务器上启用了 fopen。

编辑: allow_url_fopen 已启用,但 allow_url_include 已禁用。

有人知道我的问题是什么吗?

谢谢

【问题讨论】:

    标签: php xml wordpress fopen


    【解决方案1】:

    尝试使用绝对路径(完整路径),以及其他检查,请参阅:

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

    【讨论】:

    • 成功了。有趣的是,事后看来,解决方案似乎如此简单。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多