问题描述
我正在尝试写入Wordpress目录中的uploads文件夹中的XML文件.每次客户端更新或使用我创建的自定义post_type创建新帖子时,都需要更新此XML.
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.
这是代码:
<?
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之前的任何内容的权限.
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.
我还应该提到在服务器上启用了fopen.
I should also mention that fopen is enabled on the server.
编辑:启用allow_url_fopen但禁用allow_url_include.
allow_url_fopen is enabled but allow_url_include is disabled.
有人知道我的问题是什么吗?
Anybody know what my problem is?
谢谢
推荐答案
尝试使用绝对路径(完整路径),以及其他检查,请参见:
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);
}
这篇关于在Wordpress中使用fopen写入XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!