Page tree
Skip to end of metadata
Go to start of metadata
  • Before you try this code, make sure you have installed PHP cURL,
  • This sample uses FileCloud APIs "loginguest" and "upload"
  • This code creates custom upload form doing a single shot upload
  • This can be modified for multiple uploads as well
  • The code can upload files with same name as input or a custom name.
  • The following sample reads a file named helloworld.txt and uploads into FileCloud as hellothere.txt
 
<?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,
        "blob"
    ));

    // 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 = "http://127.0.0.1";
$pathval = "/testuser/folder";
$filename = 'hellothere.txt'; // name of the file to upload
$filefullpath = 'C:\\testfolder\\helloworld.txt'; // 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);
$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 = '1';
    $filedata = file_get_contents($filefullpath);
    list($headers, $form) = create_upload_form_for_datachunk($filedata);
    
    //make the upload call
    $url = $serverurl . "/core/upload?appname=explorer" . $appnamevalue . '&path=' . $pathvalue . '&offset=0&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);
    $result = curl_exec($ch);
    curl_close($ch);
    //end of upload call


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