13

AlfrescoからPHPでファイルを読み取り、それをブラウザーに出力しています。唯一の問題は、mimeタイプまたはファイルの拡張子です。これは私が使用しているコードです:

<?php
ob_start();
require_once("libs/AlfrescoConnect.php");

$nomeFile = rawurldecode($_GET['nomeFile']);    
$urlDownload = $_GET['urlDownload'];
$fileDownloadUrl =
    AlfrescoConnect::$serverPath. $urlDownload .
    "&attach=true&alf_ticket=".AlfrescoConnect::getTiket();
fb($fileDownloadUrl);

$cnt = file_get_contents($fileDownloadUrl);

header("Content-type: Application/octet-stream");
header('Cache-Control: must-revalidate');
header('Content-disposition: attachment; filename=' .$nomeFile);
echo($cnt);
exit();

echo("Impossibile trovare il file");

getからファイルの名前を受け取ります。これは、屋外から名前を取得する方法がわからないためです。しかし、私はどういうわけかmimetypeを推測する必要があります。私echo $cntが最初の文字である場合、それがPDFであるという事実への言及があります(たとえば、私が見る画面上に

%PDF-1.3%âãÏÓ20 ob​​j << / Length 3 0 R / Filter / CCITTFaxDecode / DecodeParms << / K 0 / Columns 2480 / Rows 3508 >> / Type / XObject / Subtype / Image / Width 2480 / Height 3508 / BitsPerComponent 1 / ColorSpace /DeviceGray>>ストリーム

したがって、関数を使用してmimeタイプを取得する方法が必要です。

どんな助けでも大歓迎です!

編集。ここに興味がある人は、mimeタイプから拡張機能を取得するために使用できるクラスです。 http://www.ustrem.org/en/articles/mime-type-by-extension-en/

4

7 に答える 7

15

finfo::buffer()次の方法を使用できます:http: //php.net/finfo_buffer

<?php
$finfo = new finfo(FILEINFO_MIME);
echo $finfo->buffer($cnt) . PHP_EOL;

:オブジェクト指向の方法論を使用するよりも適切な場合は、オプションでfinfo_buffer手続き型関数を使用できます。

于 2012-02-09T00:01:05.603 に答える
9

MIME タイプを推測 (自動検出) する必要はありません。

最後の呼び出し (またはwrapperを使用した任意の呼び出し)$http_response_headerのヘッダーを取得するために使用します。file_get_contentshttp[s]://

$contents = file_get_contents("https://www.example.com/");
$pattern = "/^content-type\s*:\s*(.*)$/i";
if (($header = array_values(preg_grep($pattern, $http_response_header))) &&
    (preg_match($pattern, $header[0], $match) !== false))
{
    $content_type = $match[1];
    echo "Content-Type is '$content_type'\n";
}
于 2018-02-14T14:23:49.347 に答える
0

これをクラスに入れます:

/**
 * Given a string ($data) with a file's contents, guess and return the mime type
 *
 * Uses the standard unix program /usr/bin/file to handle the magic (pun intended)
 *
 * @param string $data
 */
public static function get_string_mime_type($data) {
    $file_cmd = '/usr/bin/file --brief --mime-type --no-buffer -';
    return rtrim(self::exec_write_read($file_cmd, $data));
}

/**
 * Executes $cmd, writes to $cmd's stdin, then returns what $cmd wrote to stdout
 */
private static function exec_write_read($cmd, $write, $log_errors = false) {
    $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that $cmd will read from
        1 => array("pipe", "w"),  // stdout is a pipe that $cmd will write to
        2 => array("pipe", "w"),  // stderr is a pipe that $cmd will write to
    );

    $process = proc_open($cmd, $descriptorspec, $pipes);
    if (is_resource($process)) {
        // $pipes now looks like this:
        // 0 => writeable handle connected to child stdin
        // 1 => readable handle connected to child stdout
        // 2 => readable handle connected to child stderr

        fwrite($pipes[0], $write);
        fclose($pipes[0]);

        $output = stream_get_contents($pipes[1]);
        fclose($pipes[1]);

        if( $log_errors ){
            error_log(stream_get_contents($pipes[2]));
        }
        fclose($pipes[2]);

        // It is important that you close any pipes before calling
        // proc_close in order to avoid a deadlock
        $exit_code = proc_close($process);

        return $output;
    }
    else {
        throw new Exception("Couldn't open $cmd");
    }
}
于 2012-02-02T23:25:20.580 に答える
-1

file_get_contents の代わりに cURL を使用すると、レスポンス ヘッダーが表示されます。これには、MIME タイプが含まれていることが期待されます。

または、このhttp://www.php.net/manual/en/ref.fileinfo.phpまたはこの非推奨関数http://php.net/manual/en/function.mime-content-type.phpを使用してみてください。

于 2011-01-12T17:15:08.343 に答える
-1

Drupal の filefield_sources モジュールからの curl 実装を次に示します。おそらくどこでも機能します:

<?php
  // Inspect the remote image
  // Check the headers to make sure it exists and is within the allowed size.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADERFUNCTION, '_filefield_source_remote_parse_header');
  // Causes a warning if PHP safe mode is on.
  @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);

/**
 * Parse cURL header and record the filename specified in Content-Disposition.
 */
function _filefield_source_remote_parse_header(&$ch, $header) {
  if (preg_match('/Content-Disposition:.*?filename="(.+?)"/', $header, $matches)) {
    // Content-Disposition: attachment; filename="FILE NAME HERE"
    _filefield_source_remote_filename($matches[1]);
  }
  elseif (preg_match('/Content-Disposition:.*?filename=([^; ]+)/', $header, $matches)) {
    // Content-Disposition: attachment; filename=file.ext
    _filefield_source_remote_filename($matches[1]);
  }

  // This is required by cURL.
  return strlen($header);
}

/**
 * Get/set the remote file name in a static variable.
 */
function _filefield_source_remote_filename($curl_filename = NULL) {
  static $filename = NULL;
  if (isset($curl_filename)) {
    $filename = $curl_filename;
  }
  return $filename;
}

 ?>

MIME を取得するには:

<?php
echo $info['content_type'];
?>

コードはこちら: http://drupal.org/project/filefield_sources

于 2012-01-30T04:07:28.530 に答える
-2

注意してください!MIME タイプをチェックするだけでなく、悪意のあるコードがないかチェックしてください!!!!!!!!!

詳細: PHP: file_get_contents で画像の mimeType を取得する方法

于 2015-10-29T22:03:35.027 に答える