donglang1976
2016-10-04 10:36
浏览 107
已采纳

如何在保存之前获取文件信息

I have file contents which I am receiving from my form in string form (base64), and now before using file_put_contents() function to save the file, I wanna know the information related to it, like it's size and mimetypes and so on. So the question is how to retrieve a file's information just by it's data(base 64 string) For ex, let's say I have this string:-

Object {files: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvwA…Fb+kScAm4BFwC6yGB/w9uNEwUj3EmdQAAAABJRU5ErkJggg==", text: "hi"}files: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvwAAACQCAYAAAB08b+HAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7H0HeF7FsbZTbirNYNy7LTfZsiVZbrJkWc2ybLlLslzk3nvv4ALGYNN7h9ANofcOSSCETgiBJJCem0rKTSO5987/vjM755zvswy2gf/eG/Q9zz7nfHt29+zO2Z19Z3Z2tsmkqbOkbsZ8mTl/qUydtUAm18/D/3kydeYCqZ+3RKbg2YJla2TuopUyZ+EKmbt4FdLNl/q5i2X6nEWadlz1VBmLMGnKDKmdPkfG10yTuvq5eDZX/4/jf9zXI33N9NkyZeY84XvHT56OZ1NlzMQ6GTtpiqabXG/5mW7k2EkaRk+oleqpM2R87XR9x5iJUzTNyLHVMmpcTfSc6TwPy+JzvndiXb3Gl1SOlZKRCLgy34iq8RpYR4bR42ulYsxEGTFmgqbjs7JR46R89HgpGjFaiitCfpaBwLTDK6qksAzP9P8kKSwfJUOLKyR/+AgZUlQuA4YOl0EFZTK0pFIGDyuTQYWlkoe4vPwiGVhQjOfF+F8sQ5G+uGIMyhqlaYpGVGk5wxHHK8saXIhyikdoGpbPwHr1HzJMQ78B+VpuaeU4fS/v9f2FJZqfaVh29qBCyUHIHlQQBf7PHVyIMoZKNgLvB6B+XrbnH1hQEupYou1gPqazvPn6n6Ff3hDpnTtIsnDNzBkk/cPz3rmDpd/AodKzX570YsgZKJlI1wfxvbIHWH3wn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Z4QF38ZYgALUldJR0s5VLoOxV9+F2gH/Kveot90lsDIScMC/Ml3lDXUJuARaSwDUE1CF2tJNSsHwtAYyIL6epYdgHkBwUlAnzgMY9pkGd9Yzkr5DahSeo6v4itYdu4IXWnqPW/hXsAO9yS6B1ZSAA/7V7DdvtUvAJbBICcwCw6VtAVgGdYd/Njd/Fzz80naMKzftGeEhIX1nXXPqzyPb+rUW9K9A/u4uH93rcgm4BJYjAQf8y5G739Ul4BJYJQmQEtQmg419TtI56Gmgdb90X4I+ZUbAjzbCmwB6EahGBPqwRjt9p7seAOgH2G9DMeuuFV6TS8AlsCYScMC/Jh3tj+kScAnMIQFSgubdPIk52QGmAfQYqLts6z5EM2nPBXgloJA4fWeOAeSXugRcAi6B5UrAAf9y5e93dwm4BNZJAgD5TL1Ja/+8XoOu5Mc4ASglOBAUPARFpKvn83pcAi4Bl8AaS8AB/xp3vj+6S8AlsAQJ2BShdoOtJTTFb+kScAm4BFwC6yGB/w9uNEwUj3EmdQAAAABJRU5ErkJggg=="text: "hi"__proto__: Object

54:783

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwei2825 2016-10-04 12:00
    已采纳
    $encoded_data = 'iVBORw0KGgoAAAANSUhEUgAAAvwA…Fb+kScAm4BFwC6yGB/w9uNEwUj3EmdQAAAABJRU ....';    
    $size = strlen(base64_decode($encoded_data));
    

    remember base64 encoding increases the original data in size by about 33%

    Now we can write a function -

    function getFileSize($base64string){
        $bytes = strlen(base64_decode($base64string));
        $roughsize = (((int)$bytes) / 1024.0)* 0.67;
        return round($roughsize,2);
    }
    

    this function will return filesize in Kb.

    Now to know the Mime Type you can use Fileinfo

    $imgdata = base64_decode($encoded_data);
    $f = finfo_open();
    $mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题