2010-11-02 15 views
0

我編寫了一個「proxy.php」腳本(下面列出),它將獲取?img =參數中指定的圖像並將其打印到標準輸出。這是我的Flash應用程序在某些站點繞過缺少的crossdomain.xml所必需的。PHP fopen:無法打開流:成功+如何將提取的內容保存到文件?

它的工作原理,但我有3個問題,請。此外,我來自Perl的PHP,仍然在我的PHP知識中有很多空白(但是我意識到,stream_context_create和fpassthru可能使用了bucket brigades)。

1)在我的回調()函數中,如何將調試消息打印到PHP日誌中? (它會被重定向到/ var /日誌/在我的CentOS的機器消息)

2)爲什麼我收到錯誤消息未能打開流:成功,我懷念在回調(的情況下)也許?

PHP Warning: fopen() [<a href='function.fopen'>function.fopen</a>]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/html/proxy.php on line 19 
PHP Warning: fopen(http://i136.odnoklassniki.ru/getImage?photoId=154105499212&amp;photoType=0) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Success in /var/www/html/proxy.php on line 19 

3)因爲我的劇本經常與相同的圖像URL作爲參數調用,我想延長它,使它節省了獲取文件中的第一個呼叫一個目錄。在第一次和後續調用時,它應該將該緩存文件提供給STDOUT。你有一個建議,如何使它在記憶保存的方式?即我不想get_file_contents()

<?php 

define('MAX_SIZE', 1024 * 1024); 

$img = urldecode($_GET['img']); 
if (strpos($img, '..') !== FALSE) 
     exit('Wrong URL: ' . $img); 

$opts = array(
     'http'=>array(
       'method' => 'GET' 
     ) 
); 

$ctx = stream_context_create($opts); 
stream_context_set_params($ctx, array('notification' => 'callback')); 
$fh = fopen($img, 'r', FALSE, $ctx); 
if ($fh) { 
     fpassthru($fh); 
     fclose($fh); 
} 

function callback($code, $severity, $message, $message_code, $bytes_transferred, $bytes_total) { 
     if ($code == STREAM_NOTIFY_PROGRESS && $bytes_transferred > MAX_SIZE) 
       exit('File is too big: ' . $bytes_transferred); 

     if ($code == STREAM_NOTIFY_FILE_SIZE_IS) 
       if ($bytes_total > MAX_SIZE) 
         exit('File is too big: ' . $bytes_total); 
       else 
         header('Content-Length: ' . $bytes_total); 

     if ($code == STREAM_NOTIFY_MIME_TYPE_IS) { 
       if (stripos($message, 'image/gif') !== FALSE || 
        stripos($message, 'image/png') !== FALSE || 
        stripos($message, 'image/jpg') !== FALSE || 
        stripos($message, 'image/jpeg') !== FALSE) { 
         header('Content-Type: ' . $message); 
       } else { 
         exit('File is not image: ' . $mime); 
       } 
     } 
} 

?> 

回答

0
  1. 由於讀取整個文件一次:

    [email protected]:~$ host i136.odnoklassniki.ru 
    Host i136.odnoklassniki.ru not found: 3(NXDOMAIN) 
    
+0

沒錯,但我怎麼能擺脫「成功「的錯誤? – 2010-11-02 11:04:31

+0

我認爲是一個PHP錯誤;-) – Svisstack 2010-11-03 15:09:42

相關問題