2017-12-18 95 views
1
<?php 
echo "invoices/" . $invoiceN . "/address.txt"; 
echo file_get_contents("invoices/invoice1/address.txt") or die (file_get_contents("invoices/invoice1/backup.txt")); 
?> 

我的目標是在沒有找到第一個路徑的情況下擁有備份路徑。file_get_contents在不加載的情況下添加備份路徑

所以在上面的例子中,如果「address.txt中」不加載它會加載備份「爲Backup.txt」

+3

''該函數在失敗時返回讀取數據或FALSE。此函數可能會返回布爾FALSE,但也可能會返回一個非布爾值,其值爲FALSE。有關更多信息,請閱讀布爾部分。使用===運算符來測試這個函數的返回值。''http://php.net/manual/en/function.file-get-contents.php – sinisake

+3

['die()'](http:/ /php.net/manual/en/function.die.php)終止腳本;你確定這是你想要的嗎? '...或die()'不是錯誤處理;這只是你從一個糟糕的教程中學到的一個標誌。 – axiac

回答

2

使用if-else: -

<?php 
echo "invoices/" . $invoiceN . "/address.txt"; 
$data = file_get_contents("invoices/invoice1/address.txt"); 

if($data){ 
    echo $data; 
}else{ 
    echo $data = file_get_contents("invoices/invoice1/backup.txt") 
} 
?> 
2

你可以檢查文件在嘗試讀取它之前存在...

$fileName = "invoices/" . $invoiceN . "/address.txt"; 
if (!file_exists($filename)) { 
    $fileName = "invoices/" . $invoiceN . "/backup.txt"; 
} 
echo file_get_contents($fileName); 
相關問題