用PHP将字节保存到文件 [英] Save byte to file with php

查看:388
本文介绍了用PHP将字节保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些位图数据类型的C#程序,我想使用HTTP将其上传到我的Web服务器. 因此,我正在考虑将位图转换为数据类型的字节,然后将其作为文本发布到服务器,服务器将其另存为图像文件.

I have a c# program with some bitmap datatypes which i would like to upload to my web server with HTTP. So i am thinking of converting the bitmap to the datatype bytes and then post it as text to the server, the sever would then save it as a image file.

我将如何在PHP中做到这一点?

How would i do this in PHP?

我猜是这样的吗?但是我该如何指定保存路径位置?

I am guessing something like this? but then how would i specify the save path location?

    <?php
    $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
           . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
           . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
           . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
    $data = base64_decode($data);

    $im = imagecreatefromstring($data);
    if ($im !== false) {
        header('Content-Type: image/png');
        imagepng($im);
        imagedestroy($im);
    }
    else {
        echo 'An error occurred.';
    }
    ?>

推荐答案

$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
file_put_contents('img.png', base64_decode($data));

这篇关于用PHP将字节保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆