Uploading Files with API issue

Marani

Member
Nov 23, 2015
23
2
3
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
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463

Marani

Member
Nov 23, 2015
23
2
3
Iran
cPanel Access Level
Website Owner
Hello,

You will need to adjust your custom script so that it follows the guide and example we document at:

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

Let us know if this helps.

Thank you.
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
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463
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.
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.
 

Marani

Member
Nov 23, 2015
23
2
3
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?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463
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.
 

Marani

Member
Nov 23, 2015
23
2
3
Iran
cPanel Access Level
Website Owner

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463
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.
 

Marani

Member
Nov 23, 2015
23
2
3
Iran
cPanel Access Level
Website Owner
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.
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( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $ch, CURLOPT_USERPWD, $username . ':' . $password );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
 
// Set up a POST request with the payload.
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
 
// 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
 
Last edited:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463
Hello Mehdi,

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

// Define the filename and destination.
$upload_file = realpath("/favicon.ico");
$destination_dir = "public_html";
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.
 

Marani

Member
Nov 23, 2015
23
2
3
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.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463
Tell me something, by "/home/user123/favicon.ico" do you mean cPanel path? because I want to upload from my computer to cPanel.
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.
 

Marani

Member
Nov 23, 2015
23
2
3
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.
 

Marani

Member
Nov 23, 2015
23
2
3
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!?
 
Last edited:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463
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:

Parameter > file-* > A valid filename on the server.
You may want to update your custom code to upload the file using another method (e.g. FTP).

Thank you.
 

Marani

Member
Nov 23, 2015
23
2
3
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.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,267
463
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.