A8C_Files::upload_file PHP Method

upload_file() public method

public upload_file ( $details, $upload_type )
    function upload_file($details, $upload_type)
    {
        if (!file_exists($details['file'])) {
            $details['error'] = sprintf(__('The specified local upload file does not exist.'));
            return $details;
        }
        if ('editor_save' == $upload_type) {
            $post_url = $details['url'];
        } else {
            $url_parts = parse_url($details['url']);
            $file_path = $url_parts['path'];
            if (is_multisite() && preg_match('/^\\/[_0-9a-zA-Z-]+\\/' . str_replace('/', '\\/', $this->get_upload_path()) . '\\/sites\\/[0-9]+\\//', $file_path)) {
                $file_path = preg_replace('/^\\/[_0-9a-zA-Z-]+/', '', $file_path);
                $details['url'] = $url_parts['scheme'] . '://' . $url_parts['host'] . $file_path;
            }
            $post_url = $this->get_files_service_hostname() . $file_path;
        }
        $headers = array('X-Client-Site-ID: ' . constant('FILES_CLIENT_SITE_ID'), 'X-Access-Token: ' . constant('FILES_ACCESS_TOKEN'), 'Content-Type: ' . $details['type'], 'Content-Length: ' . filesize($details['file']), 'Connection: Keep-Alive');
        $stream = fopen($details['file'], 'r');
        $ch = curl_init($post_url);
        curl_setopt($ch, CURLOPT_PUT, true);
        curl_setopt($ch, CURLOPT_INFILE, $stream);
        curl_setopt($ch, CURLOPT_INFILESIZE, filesize($details['file']));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10 + (int) (filesize($details['file']) / 512000));
        // 10 plus 1 second per 500k
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_READFUNCTION, function ($ch, $fd, $length) use($stream) {
            $data = fread($stream, $length);
            if (null == $data) {
                return 0;
            } else {
                return $data;
            }
        });
        $ret_data = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        fclose($stream);
        register_shutdown_function('unlink', $details['file']);
        switch ($http_code) {
            case 200:
                if (0 < strlen($ret_data)) {
                    $obj = json_decode($ret_data);
                    if (isset($obj->filename) && basename($obj->filename) != basename($post_url)) {
                        $uploads = wp_upload_dir();
                        if (false === $uploads['error']) {
                            @copy($details['file'], $uploads['path'] . '/' . $obj->filename);
                            register_shutdown_function('unlink', $uploads['path'] . '/' . $obj->filename);
                        }
                        $details['file'] = str_replace(basename($post_url), basename($obj->filename), $details['file']);
                    }
                }
                break;
            case 204:
                $details['error'] = sprintf(__('You have exceeded your file space quota.'));
                break;
            default:
                $details['error'] = sprintf(__('Error uploading the file to the remote servers: Code %d'), $http_code);
                break;
        }
        return $details;
    }