2017-06-22 23 views
-1

所以我在另一個網站上有一個密碼保護的文件夾,我試圖把PDF從我的cliet的網站上發佈。 (是的,我有第三方的許可來做到這一點)。文件名包括一個時間戳,通常格式化爲今天的日期,但有時他們搞砸了或錯過了一天;所以,當這種情況發生時,我需要一個回退頁面。如何使用header('Content-Type:application/pdf')創建file_get_contents的異常頁面;

問題是,我似乎無法弄清楚爲什麼它不能告訴它何時得到404錯誤。

我的代碼看起來像現在這樣的權利:

<?php 

$username="XXXXXX"; 
$password="XXXXXX"; 
$source="http://The/web/site/"; 
$thisPDF="todays%20report%20".date("F%20d%20Y").".pdf"; 
$thisfallback="fallback.html"; 
$url= $source.$thisPDF; 
$fallbackURL = $source.$thisfallback; 


$context = stream_context_create(array(
     'http' => array(
      'header' => "Authorization: Basic " . base64_encode("$username:$password") 
     ) 
)); 

$str = file_get_contents($url, false, $context); 
echo $str; 
if (strpos($str, 'HTTP request failed! HTTP/1.1 404 Not Found') == false) { 
    $context = stream_context_create(array(
     'http' => array(
      'header' => "Authorization: Basic " . base64_encode("$username:$password") 
     ) 
    )); 
    $str = file_get_contents($fallbackURL, false, $context); 
    echo $str; 
} else { 
    header('Content-Type: application/pdf'); 
    $context = stream_context_create(array(
     'http' => array(
      'header' => "Authorization: Basic " . base64_encode("$username:$password") 
     ) 
    )); 

    $str = file_get_contents($url, false, $context); 
    echo $str; 
} 
?> 

回答