V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JingNi
V2EX  ›  PHP

这两种方式获取的$base64, echo 出来都一模一样,为什么一个保存的文件不正常一个保存的就正常。

  •  
  •   JingNi · 2019-07-29 02:47:14 +08:00 · 4081 次点击
    这是一个创建于 1952 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以上为代码,然后就是,写出来的 JPG 文件,前者无法正常查看并且比可正常查看的后者相差几个字节。

    10 条回复    2019-07-29 14:18:26 +08:00
    JingNi
        1
    JingNi  
    OP
       2019-07-29 02:47:33 +08:00
    <?php
    //$base64 = $_POST['content'];

    $base64 = "/9j/4AAQSkZJRgABAQEAkACQAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAACQAAAAAQAAAJAAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAAHKgAwAEAAAAAQAAAHIAAAAA/9sAQwAfFRcbFxMfGxkbIyEfJS9OMi8rKy9fREg4TnBjdnRuY21rfIyyl3yEqYZrbZvTnam4vsjKyHiV2+rZwumyxMjA/9sAQwEhIyMvKS9bMjJbwIBtgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA/8AAEQgAcgByAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A6GiiigAooqF5scL+dAEpIHU4phmQepquSWOSc0UwJvPH939aPP8A9n9ahooAsCZT1yKeCG6HNVKASDkHFAFyioEm7N+dTAgjIpALRRRQAUUUUAFFFQzv/CPxoAbLJuOB0/nUdFFMAoopyIXPH50ANoqTES9SWPtR+6b1WgCOinPGU56j1ptABTo5Ch9vSm0UAWwQRkdKWq8L4O09DVikAUUUUAIx2qT6VUJycmp5zhQPWoKYBRRRQAdalkOxQi/jUaffX606b/WGgBlFFFAEkTZ+RuhpjDaxHpQn31+tOm/1hoAZRRRQAVajbcgPeqtS255I/GgCeiiikBBcfeA9qiqS4++PpUdMAooooAKlceYoZeo6ioqkjRh82do96AI6KmZoieRk+1N3xr91Mn3oAI12je3QdKjJyST3pXcueaSgAooooAKfD/rBTKfD/rBQBZooopAQ3A6GoasyruQiq1MAoopVG5gPWgB6KFXe34CmO5c5NOmPzbR0FMoAKKKKACpNoePKjkdRUdOiba49+KAG0U6RdrkU2gAqSAfOT6Co6sQLhM+tAElFFFIAqtKm1vY1ZprqHXBoAq06MhXBPSkZSpwaSmArHLE+ppKKKACiiigAooooAfKwZsj0plFABJwOtADkXe2KtdKZGmxffvT6QBRRRQAUUUUANdA4waruhQ89PWrVFAFOip2hU9OKYYXHTBpgR0U7Y/8AdNJsb+6fyoASiniFz2xUiwAfeOaAIVUscAVYjjCD1PrTgABgDFLSAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/2Q==";

    echo $base64;

    $img = base64_decode($base64);
    $fp=fopen("test.jpg","w");
    fwrite($fp,$img);//写入

    ?>
    jamesliu96
        2
    jamesliu96  
       2019-07-29 03:51:06 +08:00 via Android
    你把它俩===一下看看
    nifury
        3
    nifury  
       2019-07-29 05:43:01 +08:00
    盲猜

    If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.
    jaskle
        4
    jaskle  
       2019-07-29 06:49:21 +08:00 via Android   ❤️ 1
    PHP 是世界上最好的语言
    holosola
        5
    holosola  
       2019-07-29 08:59:24 +08:00 via Android   ❤️ 2
    post 接收的时候会把内容里的+号变空格
    ipwx
        6
    ipwx  
       2019-07-29 09:50:49 +08:00
    @holosola 如果真是这样的话,那就是他发起 post 请求的那个脚本有问题,不是接受 post 请求的这个脚本有问题了。
    WuwuGin
        7
    WuwuGin  
       2019-07-29 10:04:55 +08:00
    那肯定是 post 里面的数据有问题啊
    JingNi
        8
    JingNi  
    OP
       2019-07-29 12:54:42 +08:00
    谢谢大家熬,我已经解决了,如果把 POST 的内容 url 编码后,就可以正常处理。至于是什么原因就不知道了,反正之前确实 echo 出来的东西一模一样,不是 POST 处理环节的问题。似乎和这位 @holosola 老哥说的一样,这个+可能有问题。后面解决了我就没去对比了。
    holosola
        9
    holosola  
       2019-07-29 14:14:26 +08:00 via Android
    @ipwx 是的,我的说法有问题
    rrfeng
        10
    rrfeng  
       2019-07-29 14:18:26 +08:00
    base64_urlsafe() 了解一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2966 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:44 · PVG 20:44 · LAX 04:44 · JFK 07:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.