削除する必要があるローカルファイルを作成しないであろう、このコードを試してみてください。
// Define URL
$url = "http://xxxx/123_{$matches[1]}";
// Open pointer to remote resource
if (!$remoteFP = @fopen($url, 'r')) {
header("{$_SERVER['SERVER_PROTOCOL']} 500 Internal Server Error");
exit;
}
// Get content length and type from remote server's headers
$length = $type = NULL;
foreach ($http_response_header as $header) { // Loop headers (see http://php.net/manual/en/reserved.variables.httpresponseheader.php)
list($name, $val) = explode(':', $header, 2); // Split to key/value
switch (strtolower(trim($name))) { // See if it's a value we want
case 'content-length':
$length = (int) trim($val);
break;
case 'content-type':
$type = trim($val);
break;
}
if ($length !== NULL && $type !== NULL) break; // if we have found both we can stop looping
}
// Send headers
if ($type === NULL) $type = 'text/plain';
header("Content-Type: $type");
if ($length) header("Content-Length: $length"); // Only send content-length if the server sent one. You may want to do the same for content-type.
// Open a file pointer for the output buffer
$localFP = fopen('php://output', 'w');
// Send the data to the remote party
stream_copy_to_stream($remoteFP, $localFP);
// Close streams and exit
fclose($remoteFP);
fclose($localFP);
exit;
それは一つにまっすぐエンティティを転送することができますので、このカールなどの上にfopen()
アプローチを使用していますボディが完全に受信される前にリモートサーバーの応答ヘッダーにアクセスできるようにします。これは、PHPを使用してプロキシするための最もリソース効率のよい方法です。
サーバがallow_url_fopen
を無効にしている場合は、データを出力バッファに直接渡すこともできますが、リモートサーバからヘッダを解析して転送することはできません。
あなたが 'unlink($ file);'で試したことがないのであれば、ファイルの所有権を確認してみてください。 – Prix
@Hakre - 変更を元に戻す...簡単なエラーがある場合は、コメントまたは回答として記入してください。ただし、それは自分のスクリプトではありませんので、誰かが間違った考えをしたり、等 – Wil
@Prix - 前にリンク解除を聞いたこともないので、今は読んでいません...(参考、これは私の最初のPHPスクリプトです...確かにプロはありません)。 – Wil