How can PHP be used to automatically rename downloaded files based on specific criteria?

When downloading files, it may be necessary to rename them based on specific criteria such as adding a timestamp or user ID to the file name. This can be achieved using PHP by first downloading the file using functions like file_get_contents() or cURL, then renaming the file using the rename() function with the desired criteria.

// Download the file
$fileUrl = 'http://example.com/file.pdf';
$fileData = file_get_contents($fileUrl);

// Rename the file based on specific criteria
$newFileName = 'new_filename_' . time() . '.pdf'; // Add timestamp to the file name
file_put_contents($newFileName, $fileData);

// Alternatively, you can add user ID to the file name
$userId = 123;
$newFileName = 'user_' . $userId . '_file.pdf';
file_put_contents($newFileName, $fileData);