Are there best practices for handling file uploads between servers in PHP?
When handling file uploads between servers in PHP, it is best practice to use secure methods such as using HTTPS for transferring files, validating file types and sizes to prevent malicious uploads, and implementing proper error handling to ensure the upload process is smooth and reliable.
// Example code for handling file uploads between servers in PHP
$sourceFile = 'path/to/source/file.jpg';
$destinationFile = 'https://destination-server.com/upload.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $destinationFile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => new CURLFile($sourceFile)
));
$response = curl_exec($ch);
if($response === false){
echo 'Error: ' . curl_error($ch);
} else {
echo 'File uploaded successfully.';
}
curl_close($ch);