symfony post api需要太多时间来加载

时间:2018-11-30 08:41:08

标签: php performance file symfony

我有一个api,它将用户的图像上传到服务器。

它将以base64格式拍摄图像并将其发送到服务器。但是问题是,对于某些用户而言,这将花费很长时间,并且对于某些用户而言,它将运行良好。

我不明白为什么会这样。但是目标目录有700GB的数据。

上传代码: ` $ file将具有base64格式的图像 $ 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 个答案:

答案 0 :(得分:0)

我不确定对base64编码的字符串执行preg_match是否是个好主意,尽管我不确定是否可以解决所有有关速度的问题,但我很肯定对base64编码的字符串实施不同的检查会提高速度。

替换以下内容:

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

与此

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