How to send a file over http with php?

by mallory_cormier , in category: PHP General , a year ago

How to send a file over http with php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , a year ago

@mallory_cormier 

To send a file over HTTP with PHP, you can use the file_get_contents function to read the contents of the file, and then use the file_put_contents function to send the file contents to a remote server using a POST request. Here's an example code snippet to demonstrate how to send a file over HTTP with PHP:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$fileUrl = 'path/to/file.txt';
$remoteUrl = 'http://example.com/upload.php';

// Read the contents of the file
$fileContents = file_get_contents($fileUrl);

// Send the file contents to the remote server using POST request
$response = file_put_contents($remoteUrl, $fileContents);

if($response !== false){
    echo 'File sent successfully';
} else {
    echo 'Failed to send file';
}


In the above code snippet, replace 'path/to/file.txt' with the path to the file you want to send, and 'http://example.com/upload.php' with the URL of the remote server where you want to send the file. You can also customize the code further to handle errors and add authentication or additional headers to the HTTP request as needed.

Related Threads:

How to upload file via guzzle http in laravel?
How to redirect media file http to https?
How to proxy a http request completely by php?
How to read from a file in PHP?
How to send a txt file in discord.js?
How to make http request using php in wordpress plugin?