Uploading Files Using PHP

This is a sample of PHP script uploading files to Plesk. Change HOST, LOGIN, PASSWD, and FILENAME with credentials of your Plesk server.

 

HOST

The IP address or name of the Plesk server.

LOGIN

Login name of the Plesk Administrator.

PASSWD

Password of the Plesk Administrator.

FILENAME

Full name of the file to be uploaded.

 

<?php
define("HOST", "10.58.97.31");
define("PATH", "/enterprise/control/agent.php");
define("LOGIN", "admin");
define("PASSWD", 'setup');
define("FILENAME", '../../data.rpm');
 
function write_callback($ch, $data)
{
echo $data;
return strlen($data);
}
 
function uploadFile($filename)
 
{$url = "https://" . HOST . ":8443" . PATH;
 
$headers = array(
"HTTP_AUTH_LOGIN: " . LOGIN,
"HTTP_AUTH_PASSWD: " . PASSWD,
"HTTP_PRETTY_PRINT: TRUE",
"Content-Type: multipart/form-data;",
);
 
// Initialize the curl engine
$ch = curl_init();
 
// Set the curl options
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// this line makes it work under https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
// Set the URL to be processed
curl_setopt($ch, CURLOPT_URL, $url);
 
curl_setopt($ch, CURLOPT_POSTFIELDS, array
('sampfile'=>"@$filename"));
 
$result = curl_exec($ch);
 
if (curl_errno($ch)) {
echo "\n\n-------------------------\n" .
"cURL error number:" .
curl_errno($ch);
echo "\n\ncURL error:" . curl_error($ch);
}
 
curl_close($ch);
 
//fclose($fp);

return;
}
 
uploadFile(realpath(FILENAME));
 
?>