0

ファイルのアップロードがあるプロジェクトに取り組んでいます。これらのファイルにはランダムな名前が付けられ、Web ディレクトリの外に配置されます。

次に、URL で渡されたパラメーターに基づいてこの画像を取得するスクリプトを用意しましたが、これはまったく問題なく機能します。

ただし、ブラウザーに渡したいヘッダーの配列を作成し、これらをforeachループしてループしましたが、何も設定されていないようです。しかし、機能を使用して手動で設定すると、header機能します! 奇妙な!

さて、headerループする代わりに関数を使用してもかまいませんが、これが機能しない特定の理由はありますか?

また、上記の方法を使用して、すべてのヘッダーを配列に格納し、コンテンツをブラウザーに配信するときにそれらを処理しますが、前述の問題を見て、それらが実際に渡されているかどうかはわかりません!

私のコードのスニペット:

// Expiry date in seconds, in this instance a year
$expiry_date=   ( 60 * 60 * 24 * 365 );
// Our headers
$img_headers=   array(
    'Content-Type : ' . $mime,
    'Cache-Control: no-cache, must-revalidate',
    //'Cache-control: max-age=' . $expiry_date,
    'Expires:' . gmdate( DATE_RFC1123, time() + $expiry_date ),
    'Pragma: ',
    'Last-Modified: ' . gmdate( gmdate( DATE_RFC1123 ), filemtime( $image_path ) ),
    'Content-Length: ' . ( string ) filesize( $image_path ),
    'Content-Disposition: inline; filename="' . $image_name . '"'
);            

/*header( 'Content-Type: ' . $mime, TRUE );
header( 'Cache-Control: no-cache, must-revalidate', TRUE );
header( 'Expires: ' . gmdate( DATE_RFC1123, time() + $expiry_date ) );
header( 'Pragma: ', TRUE );
header( 'Last-Modified: ' . gmdate( gmdate( DATE_RFC1123 ), filemtime( $image_path ) ), TRUE );
header( 'Content-Length: ' . ( string ) filesize( $image_path ), TRUE );*/

// Loop through and set up our headers!
foreach( $img_headers as $new_header )
{
    header( $new_header, TRUE );
}

// Read our image and end the rest of the file execution
return die( readfile( $image_path ) );

場違いに見えるものはありますか?

前もって感謝します!

4

2 に答える 2

0

foreach ループを次のように置き換えてみてください。空白が論理エラーを作成している場合に同様の問題が発生しました。試してみる価値があります。

foreach( $img_headers as $new_header )
{
    header( trim($new_header), TRUE );
}
于 2012-04-15T01:32:57.947 に答える