dongpalou5352 2016-09-29 08:39
浏览 172
已采纳

PHP:如何在使用base64解码后正确使用pathinfo

I'm getting an error Array to string conversion at pathinfo after using base64 decode image. Error occurs when trying to put my files into diretory. I need to maintain the name of the image.

BTW my image files came from js DOM

var formG = new FormData();
for(var i=0; i<imageTrust.length;i++){
    formG.append('file_multiImage[]', imageTrust[i]);
}


if(isset($_POST['file_multiImage'])){
    $multi_image =  $_POST['file_multiImage'];
    foreach ($multi_image as $key => $value) {
        $staticName = $event_id.'conNo'.$finalContestantNum;
        $data = str_replace('data:image/jpeg;base64,', '', $value);
        $data = str_replace(' ', '+', $data);
        $data = base64_decode($data);
        $file = pathinfo( $staticName . PATHINFO_EXTENSION); //Error occurs here
        file_put_contents($theDir.'/'. $file, $data);

    }
}

The array result

Array(
       [0] => data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQA 
       [1] => data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAlgCWAAD
       [2] => data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/7QC
)
//I did not just continue strings are too long
  • 写回答

1条回答 默认 最新

  • dongmacheng3222 2016-09-29 08:54
    关注

    There is syntax issue that the parser wouldn't have detected.

    $file = pathinfo( $staticName . PATHINFO_EXTENSION);

    change to

    $file = pathinfo( $staticName , PATHINFO_EXTENSION);

    however, your next usage of $file seems invalid, creating a malformed filename for the file_put_contents() call. By specifying PATHINFO_EXTENSION you are picking just the extension from the filename, which doesn't appear to exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 问一下,定向解包是什么意思
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并