2011-11-08 14 views
1

我試圖寫入Wordpress目錄中uploads文件夾中的XML文件。每次客戶端更新或使用我創建的自定義post_type創建新帖子時,都需要更新此XML。在Wordpress中使用fopen寫入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已禁用。

有人知道我的問題是什麼?

感謝

回答

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

它的工作。有趣的是事後看來,解決方案看起來如此簡單。謝謝! – jasonaburton