Cloudinary\Uploader::upload_large PHP Method

upload_large() public static method

Upload large raw files. Note that public_id should include an extension for best results.
public static upload_large ( $file, $options = [] )
    public static function upload_large($file, $options = array())
    {
        $src = fopen($file, 'r');
        $temp_file_name = tempnam(sys_get_temp_dir(), 'cldupload.' . pathinfo($file, PATHINFO_EXTENSION));
        $upload = $upload_id = NULL;
        $chunk_size = \Cloudinary::option_get($options, "chunk_size", 20000000);
        $public_id = \Cloudinary::option_get($options, "public_id");
        $index = 0;
        $file_size = filesize($file);
        while (!feof($src)) {
            $current_loc = $index * $chunk_size;
            if ($current_loc >= $file_size) {
                break;
            }
            $dest = fopen($temp_file_name, 'w');
            stream_copy_to_stream($src, $dest, $chunk_size);
            fclose($dest);
            if (phpversion() >= "5.3.0") {
                clearstatcache(TRUE, $temp_file_name);
            } else {
                clearstatcache();
            }
            $temp_file_size = filesize($temp_file_name);
            $range = "bytes " . $current_loc . "-" . ($current_loc + $temp_file_size - 1) . "/" . $file_size;
            try {
                $upload = Uploader::upload_large_part($temp_file_name, array_merge($options, array("public_id" => $public_id, "content_range" => $range)));
            } catch (\Exception $e) {
                unlink($temp_file_name);
                fclose($src);
                throw $e;
            }
            $upload_id = \Cloudinary::option_get($upload, "upload_id");
            $public_id = \Cloudinary::option_get($upload, "public_id");
            $index += 1;
        }
        unlink($temp_file_name);
        fclose($src);
        return $upload;
    }

Usage Example

Esempio n. 1
0
 function test_large_upload()
 {
     $temp_file_name = tempnam(sys_get_temp_dir(), 'cldupload.test.');
     $temp_file = fopen($temp_file_name, 'w');
     fwrite($temp_file, "BMJ¹YŠ|xxÀ¸YaaÿÿÿÿBGRsT¸üfffüÄõ(ÿ");
     for ($i = 1; $i <= 588000; $i++) {
         fwrite($temp_file, "ÿÿÿÿÿÿÿÿÿÿ");
     }
     fclose($temp_file);
     $this->assertEquals(5880138, filesize($temp_file_name));
     $resource = Uploader::upload_large($temp_file_name, array("chunk_size" => 5243000, "tags" => array("upload_large_tag")));
     $this->assertEquals($resource["tags"], array("upload_large_tag"));
     $this->assertEquals($resource["resource_type"], "raw");
     $resource = Uploader::upload_large($temp_file_name, array("chunk_size" => 5243000, "tags" => array("upload_large_tag"), "resource_type" => "image"));
     $this->assertEquals($resource["tags"], array("upload_large_tag"));
     $this->assertEquals($resource["resource_type"], "image");
     $this->assertEquals($resource["width"], 1400);
     $this->assertEquals($resource["height"], 1400);
     #where chunk size equals file size
     $resource = Uploader::upload_large($temp_file_name, array("chunk_size" => 5880138, "tags" => array("upload_large_tag"), "resource_type" => "image"));
     $this->assertEquals($resource["tags"], array("upload_large_tag"));
     $this->assertEquals($resource["resource_type"], "image");
     $this->assertEquals($resource["width"], 1400);
     $this->assertEquals($resource["height"], 1400);
 }
All Usage Examples Of Cloudinary\Uploader::upload_large