0

私の関数では、base64文字列からデコードされた画像を保存しています。

function saveImage(){
    //magic...
    define('UPLOAD_DIR', '../webroot/img/');
    $base64string = str_replace('data:image/png;base64,', '', $base64string);
    $base64string = str_replace(' ', '+', $base64string);
    $data = base64_decode($base64string);
    $id = uniqid();
    $file = UPLOAD_DIR.$id.'.png';
    $success = file_put_contents($file, $data);
}

上記の機能は正常に動作し、画像は指定されたフォルダに保存され、破損しません。

次の関数では、画像をユーザーに強制的にダウンロードしようとしています。

function getChart($uniqid= null){
    if($uniqid){
        $this->layout = null;
        header("Content-type: image/png");
        header("Content-Disposition:attachment;filename='".$uniqid.".png'");
        readfile('../webroot/img/'.$uniqid.'.png');
        exit;
    } else exit;
}

サーバーからダウンロードした画像が破損していて表示できません。ダウンロードしたファイルをテキストエディタで開いた後、一番上に改行文字が追加されていることに気づきました。文字を削除してファイルを保存すると、文字が正しく開き、正しく表示されます。

どうすればこれを修正できますか?

4

6 に答える 6

1
header("Content-length: $file_size")

このヘッダーは、ファイルのサイズをブラウザに通知します。一部のブラウザでは、ファイルを正しくダウンロードできるようにする必要があります。とにかく、ファイルの大きさを伝えるのは良い方法です。そうすれば、ファイルをダウンロードする人は誰でも、ダウンロードにかかる時間を予測できます。

header("Content-type: $file_type")

このヘッダーは、ブラウザがダウンロードしようとしているファイルの種類を示します。

header("Content-Disposition: attachment; filename=$file_name");

これは、このダウンロードされたファイルを指定された名前で保存するようにブラウザに指示します。このヘッダーを送信しない場合、ブラウザはスクリプトの名前を使用してファイルを保存しようとします。

ただし、最初の出口の真上flush();で、または場合によっては、バッファをフラッシュする必要があります。ob_flush();

于 2012-09-28T12:03:30.280 に答える
1

あなたが説明することは、あなたが実際にダウンロードされたファイルを開くまで隠されている複数の問題を抱えている可能性があります。

代わりに、コードをより堅牢にし、前提条件を確認します。ここで、ヘッダーがすでに送信されている場合は、既存の出力バッファーをクリーンアップし、それが不可能な場合はエラーを出します。

function getChart ($uniqid = null) {

    if (!$uniqid) exit;

    $this->layout = null;

    if (headers_sent()) throw new Exception('Headers sent.');
    while (ob_get_level() && ob_end_clean());
    if (ob_get_level()) throw new Exception('Buffering is still active.');

    header("Content-type: image/png");
    header("Content-Disposition:attachment;filename='".$uniqid.".png'");
    readfile('../webroot/img/'.$uniqid.'.png');

    exit;
}
于 2012-09-28T12:07:20.333 に答える
0

編集(ダウンロードを強制するため):

function getChart($uniqid= null){
    if($uniqid){

        $image = $uniqid;

        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=" . $image);
        header("Content-Type: image/jpg");
        header("Content-Transfer-Encoding: binary");

        readfile($image);

    } else exit;
}

getChart("060620121945.jpg");

これを試してください(画像をレンダリングするためだけに):

function getChart($uniqid= null){
    if($uniqid){

        $mime_type = "image/png";
        $content = file_get_contents('../webroot/img/'.$uniqid.'.png');
        $base64   = base64_encode($content); 
        return ('data:' . $mime_type . ';base64,' . $base64);

    } else exit;
}
于 2012-09-28T12:01:55.787 に答える
0

呼び出す前に何かを出力しているかどうかを確認してくださいreadfile

// add ob_start() at the very top of your script
function getChart($uniqid= null){
    echo strlen(ob_get_clean()); die(); // if it's not 0 then you are definetly echoing something

    if($uniqid){
        $this->layout = null;
        header("Content-type: image/png");
        header("Content-Disposition:attachment;filename='".$uniqid.".png'");
        readfile('../webroot/img/'.$uniqid.'.png');
        exit;
    } else exit;
}
于 2012-09-28T12:03:49.030 に答える
0

私はこの問題を何度か経験しました。これが私が使用した解決策です:

unicode BOMほとんどの場合、ファイルのエンコードをオフにするのを忘れていたことがdownload.phpわかりました。これにより、ダウンロードしたファイルの先頭に何かが追加され、ファイルが破損します。

したがって、解決策はでオフunicode BOMにすることdownload.phpです。

于 2015-06-14T16:23:07.947 に答える
0

ob_clean();を使用するだけです。readfile()の前

于 2017-08-16T22:16:08.777 に答える