Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Uploading Files with API issue

Discussion in 'cPanel Developers' started by Marani, Apr 15, 2018.

Tags:
  1. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    Hi,

    Using POSTMAN I'm trying to upload files from local computer to the server using this method.
    This is the code I use:

    Code:
    https://secure10.mycPanel.com:2083/cpsess4228153237/execute/Fileman/upload_files?dir=/home/myUsernameHere/public_html&file-1=123.html
    And it says:

    Code:
    {"data":{"succeeded":0,"warned":0,"uploads":[],"failed":0},"messages":null,"errors":["You must specify at least one file to upload."],"metadata":{},"status":0}
    First, is my approach to uploading files from local correct?
    Second, if the answer to the first is yes, then how I correct this issue?

    Thanks
     
  2. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    Michael,

    Thanks for the link but please look at number 11 on the link you gave:
    Add content to upload to the file.

    The thing is, I want to upload a ready file. I mean in number 11 the content is getting put inside the file, but mine is a ready-to-upload file on the computer. I need to use variable inside programming code or upload the file from a local path.

    Thanks,
    Mehdi
     
  4. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello,

    Step number four on the "PHP" example shows how to define the file to upload as a local path. Let me know if that helps.

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    Hi,
    Thanks for helping Michael.

    Gave it a try and it's still the same. actually, that was what I inserted already.
    I even used a sniffer to capture the requests when uploading using cPanel as normal, and in the sniffer it was the same request I posted before.

    The POST request is correct that's strange it always returns this error?
     
  6. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @Marani,

    I created a PHP script on a cPanel server matching the example referenced here. I then created a test file, and updated the PHP code to reflect the path of the test file and to match a test cPanel account's login details. Upon executing the PHP script on the server, the test file was successfully uploaded to the test account's public_html directory.

    Can you let us know the full steps you are using when attempting to use this UAPI function?

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
  8. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @Marani,

    Can you confirm that the local file you are uploading exists on the same machine that you are running the custom application on? If so, can you try setting up the PHP script referenced in our documentation (either locally or on the cPanel server) and then attempting to upload the same file? This way we can rule out if it's an issue with your custom application or if it's some other issue with the file upload process.

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    Hi Michael, sorry I've been so busy I couldn't come back and answer. I just tried what you said here is the result:

    On local using xampp installation it says:

    Code:
    The cURL call did not return valid JSON: 
    Fatal error: in C:\xampp\htdocs\uploadtest.php on line 49
    Line 49: die( $response );

    And uploading the file on the server returns error 500:

    This page isn’t working
    mysite.com is currently unable to handle this request.

    HTTP ERROR 500

    But the site itself works, it shows up with no error.

    Here the PHP code I tool from the sample link:
    PHP:
    <?php
    // Log everything during development.
    // If you run this on the CLI, set 'display_errors = On' in php.ini.
    error_reporting(E_ALL);
     
    // Declare your username and password for authentication.
    $username 'myUserHere';
    $password 'PassHere';
     
    // Define the API call.
    $cpanel_host 'mysite.com';
    $request_uri "https://$cpanel_host:2083/execute/Fileman/upload_files";
     
    // Define the filename and destination.
    $upload_file realpath("/favicon.ico");
    $destination_dir "public_html";
     
    // Set up the payload to send to the server.
    if( function_exists'curl_file_create' ) ) {
        
    $cf curl_file_create$upload_file );
    } else {
        
    $cf "@/".$upload_file;
    }
    $payload = array(
        
    'dir'    => $destination_dir,
        
    'file-1' => $cf
    );
     
    // Set up the cURL request object.
    $ch curl_init$request_uri );
    curl_setopt$chCURLOPT_HTTPAUTHCURLAUTH_BASIC );
    curl_setopt$chCURLOPT_USERPWD$username ':' $password );
    curl_setopt$chCURLOPT_SSL_VERIFYHOSTfalse );
    curl_setopt$chCURLOPT_SSL_VERIFYPEERfalse );
     
    // Set up a POST request with the payload.
    curl_setopt$chCURLOPT_POSTtrue );
    curl_setopt$chCURLOPT_POSTFIELDS$payload );
    curl_setopt$chCURLOPT_RETURNTRANSFERtrue );
     
    // Make the call, and then terminate the cURL caller object.
    $curl_response curl_exec$ch );
    curl_close$ch );
     
    // Decode and validate output.
    $response json_decode$curl_response );
    if( empty( 
    $response ) ) {
        echo 
    "The cURL call did not return valid JSON:\n";
        die( 
    $response );
    } elseif ( !
    $response->status ) {
        echo 
    "The cURL call returned valid JSON, but reported errors:\n";
        die( 
    $response->errors[0] . "\n" );
    }
     
    // Print and exit.
    die( print_r$response ) );
    ?>
    Any ideas?

    Thanks,
    Mehdi
     
    #9 Marani, Jun 1, 2018
    Last edited: Jun 1, 2018
  10. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello Mehdi,

    Let's start with the example you uploaded to the cPanel server so we can ensure it's working as expected.

    Can you try using a file path accessible to the user you are logged in as? For example, try:

    Code:
    $upload_file = realpath("/home/user123/favicon.ico");
    Make sure /home/user123/favicon.ico exists before executing the PHP script. If the "500" error code persists when running the code in your web browser, let us know the output to /usr/local/apache/logs/error_log when you see the error.

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    Hi Michael,

    Tell me something, by "/home/user123/favicon.ico" do you mean cPanel path? because I want to upload from my computer to cPanel.

    PHP:
    // Define the filename and destination.
    $upload_file realpath("file.txt");
    $destination_dir "public_html";
    The cURL call returned valid JSON but reported errors: Failed to upload any of the requested files with various failures.
     
  12. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Yes, for the purpose of this test you should use a path under the account on the cPanel server. A local path is only suitable when the script is hosted on your local computer (we are testing the script on the cPanel server first to rule out any issues with the API function itself on your system).

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    /home/myUserName/file.txt returns: The cURL call did not return valid JSON

    but just file.txt returns what I mentioned last time:
    The cURL call returned valid JSON but reported errors: Failed to upload any of the requested files with various failures.
     
  14. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    Ok Michael!
    I changed the destination_dir and now it says:

    Code:
    stdClass Object ( [errors] => [messages] => [data] => stdClass Object ( [uploads] => Array ( [0] => stdClass Object ( [status] => 1 [size] => 0 [warnings] => Array ( ) [file] => file.txt [reason] => Upload of “file.txt” succeeded. ) ) [failed] => 0 [warned] => 0 [succeeded] => 1 ) [warnings] => [metadata] => stdClass Object ( ) [status] => 1 ) 1
    And I see the file has been copied to the new location I specified. It worked yes!?
     
    #14 Marani, Jun 5, 2018
    Last edited: Jun 5, 2018
  15. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @Marani,

    Yes, the output you provided shows the file was successfully uploaded. This shows the API function is working as intended, and the reason your custom application is failing is likely the result of a coding error. I believe the primary issue is that you are attempting to upload a file from your local computer as opposed to a file that's stored on the cPanel server. For instance, in the documentation on this API function, it states:

    You may want to update your custom code to upload the file using another method (e.g. FTP).

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. Marani

    Marani Member

    Joined:
    Nov 23, 2015
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    3
    Location:
    Iran
    cPanel Access Level:
    Website Owner
    So you say uploading file to cPanel using API is not possible? I believe for developers this is one of those most-wanted features.

    And what's the reason to upload a file which already exists in cPanel via this API? is this a replacement for like, Copy command?

    What if I use POST and GET Requests to process it in the background instead of using APIs? would it work?

    I want to create a simple uploader command for my clients.
     
  17. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @Marani,

    I apologize, I probably wasn't clear enough in my prior response. To clarify, the file itself doesn't have to exist on the cPanel server beforehand, but the UAPI function needs to receive the data associated with the file before the upload occurs. You can't enter the path to the file on your local workstation without first coding some method of sending the file data to the server. For instance, cURL is a common method of doing this with PHP when setting up a web page with a form that allows users to upload a file:

    PHP: rfc:curl-file-upload

    This is what's noted under steps 5 through 9 on the PHP example at:

    Tutorial - Use UAPI's Fileman::upload_files Function in Custom Code - Developer Documentation - cPanel Documentation

    I don't have a specific C# example to provide, but here's a StackOverflow thread that may point you in the right direction:

    Making a cURL call in C#

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice