使用Cpanel api进行文件操作

问题描述 投票:0回答:1

我有一个 PHP 代码,可以使用 API 将文件上传到我的 cpanel 帐户。

问题是这样的:如果我要上传的文件已经存在,则不会上传这个文件。

我想要的是这个:如果我要上传的文件已经存在,我希望它覆盖它。

我的代码:

<?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 = 'xxxxx';
$password = 'xxxxx';

// Define the API call.
$cpanel_host = 'xxxxxx';
$request_uri = "https://$cpanel_host:2083/execute/Fileman/upload_files";

// Define the filename and destination.
$upload_file = realpath("test.php");
$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 ) );
?>

也可以是删除旧文件然后安装新文件的过程,没关系

php file-upload
1个回答
0
投票

如果文件存在,您是否尝试将其删除并重新上传?

© www.soinside.com 2019 - 2024. All rights reserved.