How to rename file using in php using curl?

Member

by jasen , in category: PHP General , 4 months ago

How to rename file using in php using curl?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 4 months ago

@jasen 

To rename a file using PHP with CURL, you can use the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

$oldFileName = 'old_file_name.txt';
$newFileName = 'new_file_name.txt';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/' . $oldFileName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'MOVE');
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_INFILE, fopen($oldFileName, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($oldFileName));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Destination: http://example.com/' . $newFileName,
));
$response = curl_exec($ch);
curl_close($ch);

echo $response;

?>


This code sends an HTTP MOVE request to the server using CURL with the CURLOPT_CUSTOMREQUEST option set to 'MOVE'. It specifies the source file using CURLOPT_URL and the destination file using the 'Destination' header. The CURLOPT_UPLOAD option is set to true to indicate that we are uploading a file. Finally, the server response is echoed.


Make sure to replace 'http://example.com/' with the actual URL of your server, and specify the actual names of the old and new files.