php upload file

  • 1
  • Question
  • Updated 7 months ago
I am trying to find out how to use fopen along with function with the example upload_file($token, $folder_id, $local_path)


First, my fopen:
$request_file = "./" . $tempFileName;
$fh = fopen($request_file, 'r');
$xl_data = fread($fh, filesize($request_file));

As a result, I have modified the upload_file function:
function upload_file($token, $folder_id, $local_path, $xl_data){
 $uri = "https://".get_hostname($token)."/sf/v3/Items(".$folder_id.")/Upload";
echo "GET ".$uri."\n";

$headers = get_authorization_header($token);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$curl_response = curl_exec ($ch);

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error_number = curl_errno($ch);
$curl_error = curl_error($ch);

$upload_config = json_decode($curl_response); echo "xl file: " . var_dump($xl_data);

if ($http_code == 200) {
// $post["File1"] = new CurlFile($local_path);
curl_setopt ($ch, CURLOPT_URL, $upload_config->ChunkUri);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $xl_data);
curl_setopt ($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_HEADER, true);

$upload_response = curl_exec ($ch);

echo $upload_response."\n";
}
curl_close ($ch);
}

I am not receiving an error, but I am not seeing the file either. The var_dump shows that there is a binary file, so why is it not uploading?

Do I have to change $uri = "https://".get_hostname($token)."/sf/v...";

To the Shared Folder?
Photo of Tarik Abdel-Monem

Tarik Abdel-Monem

  • 13 Posts
  • 1 Reply Like

Posted 7 months ago

  • 1
Photo of Nikita Nuka

Nikita Nuka, Employee

  • 10 Posts
  • 2 Reply Likes
Hello, Tarik.
Hope you are doing great.

I believe there might some missing pieces in your code.
  • "Items/Upload" endpoint should be called using POST HTTP method. You will have to add this line to your first HTTP call: curl_setopt ($ch, CURLOPT_POST, true)
  • After you send all file bytes to $upload_config->ChunkUri, you should also call $upload_config->FinishUri if it's provided.

Please, see "Upload File" section of ShareFile API documentation: https://api.sharefile.com/rest/docs/resource.aspx?name=Items

Please, let us know if there are any other questions.

Best Regards,
Nikita Nuka.
Photo of Tarik Abdel-Monem

Tarik Abdel-Monem

  • 13 Posts
  • 1 Reply Like
Thank you for replying Nikita,

I have tried your suggestions, but the "FTP" folder is still empty:

I have set the $folder_id to FTP, should it be Shared Folder/FTP?

    $folder_id = "FTP";

    function upload_file($token, $folder_id, $local_path, $xl_data) {
        $uri = "https://".get_hostname($token)."/sf/v3/Items(".$folder_id.")/Upload";
        echo "GET ".$uri."\n";
    
        $headers = get_authorization_header($token);
    
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $uri);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt ($ch, CURLOPT_VERBOSE, FALSE);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt ($ch, CURLOPT_POST, true);
    
        $curl_response = curl_exec ($ch);
    
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curl_error_number = curl_errno($ch);
        $curl_error = curl_error($ch);
    
        $upload_config = json_decode($curl_response);
    
        // echo "xl file: " . var_dump($xl_data);
    
        if ($http_code == 200) {
            // $post["File1"] = new CurlFile($local_path);
            curl_setopt ($ch, CURLOPT_URL, $upload_config->ChunkUri);
            curl_setopt ($ch, CURLOPT_URL, $upload_config->FinishUri);
            curl_setopt ($ch, CURLOPT_POST, true);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $xl_data);
            curl_setopt ($ch, CURLOPT_VERBOSE, FALSE);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt ($ch, CURLOPT_HEADER, true);
    
            $upload_response = curl_exec ($ch);
    
            echo $upload_response."\n";
        }
        curl_close ($ch);
    }
Photo of Tarik Abdel-Monem

Tarik Abdel-Monem

  • 13 Posts
  • 1 Reply Like
OK, I made some changes and I did an echo on the http code and var dump on the $post["File1"]

As a result, I am receiving a 411 - content length error - how do I send the content length of the file? Also, how do I make sure the rest of the $post array is populated or does it matter?

http_code: 411

upload_response

class CURLFile#3 (3) {

  public $name =>

  string(44) "2017-11-09 11:13:16-financial_resources.xlsx"

  public $mime =>

  string(0) ""

  public $postname =>

  string(0) ""

}
Photo of Nikita Nuka

Nikita Nuka, Employee

  • 10 Posts
  • 2 Reply Likes
Thank you, Tarik.
A few more things to fix.

"$folder_id" should contain an ID of "FTP" folder. You can get it using the following code:

$uri = "https://".get_hostname($token)."/sf/v3/Items/ByPath?path=/FTP";
echo "GET ".$uri."\n";
$headers = get_authorization_header($token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curl_response = curl_exec ($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Response code ".$http_code."\n";
$folder = json_decode($curl_response);
echo "Folder ID ".$folder->Id;

As our documentation reads: "For all uploaders, the contents of the POST Body can either be "raw" (if the parameter raw=true is provided to the Uploader) or use MIME multi-part form encoding otherwise. Raw uploads provide the chunk content directly in the POST body and the Content-Length header specifies the size. Multi-part form encoding requires passing the file as a Form parameter named "Filedata"."

In your code, you don't add the "raw" parameter, and ShareFile expects data to be in the multi-part form. In order to fix the issue, you will need to add the "raw" parameter to the upload URL and also specify a file name. Here is a sample of a raw upload:

$uri = "https://".get_hostname($token)."/sf/v3/Items(".$folder_id.")/Upload?raw=true&...;
echo "GET ".$uri."\n";

$headers = get_authorization_header($token);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$curl_response = curl_exec ($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Response code ".$http_code."\n";

$upload_config = json_decode($curl_response);
echo "Chunk URI ".$upload_config->ChunkUri."\n";

if ($http_code == 200) {
    curl_setopt ($ch, CURLOPT_URL, $upload_config->ChunkUri);
    curl_setopt ($ch, CURLOPT_POST, true);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $xl_data);
    curl_setopt ($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_HEADER, true);
    $upload_response = curl_exec ($ch);
    echo $upload_response."\n";
}

Best Regards,
Nikita Nuka.
(Edited)
Photo of Tarik Abdel-Monem

Tarik Abdel-Monem

  • 13 Posts
  • 1 Reply Like
Thanks for your response and I have made the modifications that you have suggested

You mention your documentation, this is where I am going and I am not finding what you are refering to - https://api.sharefile.com/rest/samples/php.aspx -- can you provide a link?

Here are my changes, I am not receiving any errors, but I  also do not see a file uploaded to the FTP folder.

Although $upload_config->ChunkUri is empty, even when I tried a var dump

function upload_file($token, $folder_id, $local_path, $xl_data) {
        $uri = "https://" . get_hostname($token) . "/sf/v3/Items/ByPath?path=/FTP";
        echo "GET " . $uri . "\n";
    
        $headers = get_authorization_header($token);
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
        $curl_response = curl_exec ($ch);
    
        $upload_config = json_decode($curl_response);
    
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        echo "Response code " . $http_code . "\n";
    
        $folder = json_decode($curl_response);
        echo "Folder ID " . $folder->Id . "\n";
    
        $uri = "https://" . get_hostname($token) . "/sf/v3/Items(" . $folder->Id . ")/Upload?raw=true&fileName=" . $local_path;
        echo "GET " . $uri . "\n";
    
        echo "local_path: " . $local_path . "\n";
        echo "Chunk URI " . $upload_config->ChunkUri . "\n";
    
        if ($http_code == 200) {
            curl_setopt ($ch, CURLOPT_URL, $upload_config->ChunkUri);
            curl_setopt ($ch, CURLOPT_POST, true);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $xl_data);
            curl_setopt ($ch, CURLOPT_VERBOSE, FALSE);
            curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt ($ch, CURLOPT_HEADER, true);
            $upload_response = curl_exec ($ch);
    
            echo "upload_response" . $upload_response . "\n";
            var_dump($xl_data);
        }else{
            echo "http_code: " . $http_code . "\n";
        }
        curl_close ($ch);
    }
Photo of Nikita Nuka

Nikita Nuka, Employee

  • 10 Posts
  • 2 Reply Likes
Thank you, Tarik.
Our documentation can be found here: https://api.sharefile.com/rest/docs/resource.aspx?name=Items

Is "FTP" folder a root-level shared folder or a subfolder of your home folder? If it's a subfolder of your home folder, you should prepend the "path" parameter in the URL with your email.

Here is a modified version of your method that works for me.

function upload_file($token, $folder_path, $local_path, $xl_data) {
    $uri = "https://" . get_hostname($token) . "/sf/v3/Items/ByPath?path=" . $folder_path;
    echo "GET " . $uri . "\n";
    
    $headers = get_authorization_header($token);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $uri);
    curl_setopt($ch, CURLOPT_TIMEOUT, 300);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $curl_response = curl_exec ($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "Response code " . $http_code . "\n";
    
    $folder = json_decode($curl_response);
    echo "Folder ID " . $folder->Id . "\n";
    
    echo "local_path: " . $local_path . "\n";
    $uri = "https://" . get_hostname($token) . "/sf/v3/Items(" . $folder->Id . ")/Upload?raw=true&fileName=" . $local_path;
    echo "GET " . $uri . "\n";
    
    curl_setopt($ch, CURLOPT_URL, $uri);
    curl_setopt ($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 300);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $curl_response = curl_exec ($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "Response code " . $http_code . "\n";
    
    if ($http_code == 200) {
        $upload_config = json_decode($curl_response);
        echo "Chunk URI " . $upload_config->ChunkUri . "\n";
        curl_setopt ($ch, CURLOPT_URL, $upload_config->ChunkUri);
        curl_setopt ($ch, CURLOPT_POST, true);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $xl_data);
        curl_setopt ($ch, CURLOPT_VERBOSE, FALSE);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/text"));
        $upload_response = curl_exec ($ch);
        echo "upload_response" . $upload_response . "\n";
    }else{
        echo "http_code: " . $http_code . "\n";
    }
    curl_close ($ch);
}
upload_file("token", "/nikita.nuka@citrix.com/PHPTest", "TestFile3.txt", "TestData");

In my case, "PHPTest" folder is a subfolder of my home folder.

Please, let me know if this code works for you.
Thank you.

Best Regards,
Nikita Nuka.
Photo of Tarik Abdel-Monem

Tarik Abdel-Monem

  • 13 Posts
  • 1 Reply Like
Hey Nikita,

It did work for my test account - application.sharefile.com

However, when I tried to send a file to myfinancialresources.sharefile.com

I received the following response:

POST https://myfinancialresources.sharefile.com/oauth/token

{"error":"invalid_grant","error_description":"invalid username or password"}

400

I was under the impression that I could use my same credentials to send to other accounts on ShareFile

I know that I was originally sent a set of credentials from Financial Resources, but I created a test account to test the code first, which lead to me saving the new password in Chrome. Now, tabldelmonem@rategenius.com is saved with the password for this trial account.

When I click on the activate link sent via Financial Resources, I am directed to login, which I am allowed in, but I believe it maybe using the application.sharefile.com password.

Can you reset my password? When I click on the "Forgot Password" link, I receive the following message:

Administrator Required

You do not have the permission to reset your password. For help, contact your administrator.
Photo of Tarik Abdel-Monem

Tarik Abdel-Monem

  • 13 Posts
  • 1 Reply Like
Hey Nikita,

I was able to have Financial Resources allow for a password reset and everything was working until it reached 

    function get_root($token, $get_children=FALSE) {
        $uri = "https://".get_hostname($token)."/sf/v3/Items";

Which results in a 404

Same with:

GET https://myfinancialresources.sf-api.com/sf/v3/Items/ByPath?path=/tabdelmonem@rategenius.com/FTP

Response code 404
Photo of Tarik Abdel-Monem

Tarik Abdel-Monem

  • 13 Posts
  • 1 Reply Like
Hey Nikita,

You can close this ticket.

When I removed the "/tabdelmonem@rategenius" from the folder_path, then everything worked and the file was uploaded

upload_file($token, "/FTP", $local_path, $xl_data);

Thank you for all your help. I do not know how I would have figured it out without you. Thank you once more