Is it possible to upload a file without using a form in PHP?

Yes, it is possible to upload a file without using a form in PHP by sending a POST request with the file content as the request body. This can be achieved using cURL or other HTTP client libraries to make the request to the server with the file content.

<?php

// Specify the file path to upload
$file_path = '/path/to/file.txt';

// Specify the URL to upload the file to
$upload_url = 'https://example.com/upload.php';

// Open the file for reading
$file_handle = fopen($file_path, 'r');

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, fread($file_handle, filesize($file_path)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Close cURL session and file handle
curl_close($ch);
fclose($file_handle);

// Output the response
echo $response;

?>