dongzhiyi2006 2018-11-30 08:41
浏览 29

symfony post api花费了太多时间来加载

I have a api, which will upload the images of the users to server.

It will take the images in base64 format and send that to server. But the problem is for some user it will take long time, and for some user it works well.

I am not getting why this is happening. But the destination directory is a having 700GB of data.

code for uploading : ` $file will be having base64 format of image $this->file = $file;

    if ($this->id && !empty($this->path) && !is_null($file)) {
        $this->storeFilenameForRemove();
    }

    if ($file instanceof File) {

        if (isset($this->path)) {
            $this->temp = $this->path;
            $this->path = null;
        } else {
            $this->path = 'initial';
        }
    } else if (gettype($file) == 'string') {

        if (preg_match('/data:(\w+)\/(\w+);base64,/i', $file, $matches)) {

            if ($matches) {

                $file = preg_replace('/data:(\w+)\/(\w+);base64,/i', '', $file);

                $tmpFile = Array();
                $tmpFile['data'] = base64_decode( str_replace(' ', '+', $file) );

                if ($matches[1] === 'image') {

                    $tmpFile['name'] = uniqid().'.png';
                } else {

                    $tmpFile['name'] = uniqid().'.'.$matches[2];
                }

                $tmpFile['handle'] = fopen( $this->getUploadRootDir().'/'.$tmpFile['name'], 'w' );

                // inject the raw image data into the new file
                fwrite( $tmpFile['handle'], $tmpFile['data'] );
                fclose( $tmpFile['handle'] );

                $this->path = $tmpFile['name'];
            }
        }
    } else {

        $this->file = $file;
    }`
  • 写回答

1条回答 默认 最新

  • douwei7976 2018-11-30 11:43
    关注

    I'm not sure if executing a preg_match on a base64 encoded string is a good idea, while I'm not sure that fixes all of your problems regarding speed I'm positive that implementing a different check for base64 encoded strings would improve the speed.

    Replace the following:

    if (preg_match('/data:(\w+)\/(\w+);base64,/i', $file, $matches)) {
    

    with this

    if ( base64_encode(base64_decode($file)) === $file){
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?