PHP Sample For Multiple Chunk Upload

  • This sample does multi-chunked uploading.
  • Before you try this code, make sure you have installed PHP cURL,
  • This sample uses FileCloud APIs "loginguest" and "upload".
  • This code can be used to upload large files, splitting them into 20MB chunks.
  • The following sample reads a file named sample.mp4 and uploads it into FileCloud as sample.mp4
<?php

    function create_upload_form_for_datachunk($data) {
    $headers = array();
    $form[] = implode("\r\n", array(
        "Content-Disposition: form-data; name=\"filedata\"; filename=\"blob\"",
        "Content-Type: application/octet-stream",
        "",
        $data
    ));

    // generate safe boundary
    do {
        $boundary = "---------------------" . md5(mt_rand() . microtime());
    } while (preg_grep("/{$boundary}/", $form));

    // add boundary for each parameters
    array_walk($form, function (&$part) use ($boundary) {
        $part = "--{$boundary}\r\n{$part}";
    });

    // add final boundary
    $form[] = "--{$boundary}--";
    $form[] = "";

    // set options
    $headers[] = "Content-Type: multipart/form-data; boundary={$boundary}";
    return array($headers, $form);
}

$cookie_jar = tempnam('C:\\testfolder', 'cookie');

// Input Options
$username = "testuser";
$password = "password123";
$serverurl = "https://127.0.0.1";
$pathval = "/testuser/folder";
$filename = 'sample.mp4'; // name of the file to upload
$filefullpath = 'C:\\testfolder\\'.$filename; // Full path to file to upload
//assign post data
$param = array('userid' => $username, 'password' => $password);

//login to the site
$api = "loginguest";
$url = $serverurl . "/core/" . $api;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
//end of login

$xmlstr = new \SimpleXMLElement($data);
$result = $xmlstr->command->result;
if ($result == 1) {
    echo "Profile Logged in Successfully";

    //upload api call
    $api = "upload";
    $appnamevalue = "explorer";
    $pathvalue = $pathval;


    //creating a single shot upload call
    $offset = 0;
    $complete = '0';
    $file_sz = filesize($filefullpath);
    $chunk_sz = 20000000; //20MB chunks

    while($offset < $file_sz) {
        if(($offset + $chunk_sz) >= $file_sz) {
            $complete = '1';
            $sz = $file_sz - $offset;
        } else {
            $sz = $chunk_sz;
        }

        $data = file_get_contents($filefullpath, FALSE, NULL, $offset, $sz);
        $tt = strlen($data);
        list($headers, $form) = create_upload_form_for_datachunk($data);

        $url= $serverurl."/core/upload?appname=explorer" . '&path=' . $pathval . '&offset=' . $offset . '&complete='
            . $complete. '&filename=' . $filename;

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, implode("\r\n", $form));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $result = curl_exec($ch);
        curl_close($ch);

        if ($result != "OK") {
            echo "Failed to upload\n";
            exit;
        }

        $offset = $offset + $chunk_sz;
    }



    if ($result == "OK") {
        echo "</br>" . "File uploaded Successfully";
    } else {
        echo $result;
    }
} else {
    echo "Error:" . $xmlstr->command->message;
}